Advertisement
Guest User

Qt Contador

a guest
Dec 6th, 2011
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.73 KB | None | 0 0
  1. /*
  2.  * (C) 2011 Angelo S. Mavridis Bartolome <barklome@gmail.com>
  3.  * This program is free software; you can redistribute it and/or modify
  4.  * it under the terms of the GNU General Public License as published by
  5.  * the Free Software Foundation; either version 2 of the License, or
  6.  * (at your option) any later version.
  7.  *
  8.  * This program is distributed in the hope that it will be useful,
  9.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  11.  * GNU General Public License for more details.
  12.  *
  13.  * You should have received a copy of the GNU General Public License
  14.  * along with this program; if not, write to the Free Software
  15.  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301  USA
  16.  */
  17.  
  18. /*
  19.   Tutoral Qt : Contador de palavras
  20.   Escrito por: Angelo S. Mavridis Bartolome
  21.   Livre para copiar, só não pode falar que você quem fez :)
  22.   */
  23.  
  24. #include <QDebug>
  25. #include <QFile>
  26.  
  27. int main(int argc, char *argv[])
  28. {
  29.     QFile f(argv[1]); // Cria instancia do QFile
  30.     int contar = 0;  // Cria um contador
  31.  
  32.     if(argc < 3)
  33.     {
  34.         qDebug() << "Uso: ./aplicativo ARQUIVO PALAVRA";
  35.         return 1; // Retorna 1: Falha. Retorna 0: Sucesso
  36.     }
  37.  
  38.     if(!f.open(QIODevice::ReadOnly | QIODevice::Text))
  39.     {
  40.         qDebug() << "Falha ao abrir o arquivo: " << f.fileName();
  41.         return 1; // Retorna falha novamente
  42.     }
  43.  
  44.  
  45.     QTextStream arquivoTexto(&f);
  46.     QString linha;
  47.  
  48.     while(!arquivoTexto.atEnd())
  49.     {
  50.         linha = arquivoTexto.readLine();
  51.         contar = contar + linha.count(QString(argv[2]));
  52.     }
  53.  
  54.     qDebug() << "Lido: " << f.fileName() << ". Contado " << contar << argv[2] << "(s)";
  55.  
  56. }
  57.  
  58.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement