2012-02-19 14 views
0

Qt4でテキストファイルを1単語ずつ読みたいです。正直言って新しくなっています。別のファイルに1行に1単語を書きたいと思います。私はこれをC++で問題なく実行できましたが、ifstreamとofstreamをQt4で使用しようとしたときにエラーが発生しました。ここに私のQt4をコードは(ボタンのイベントリスナーに)Qt4のテキストファイルから1ワードずつ読む

// a string that holds the text inside the File Name index 
    QString str = ui->txtIndex->toPlainText(); 

    // Check if this is a text file 
    if(str.endsWith(".txt")){ 
     qDebug() << "a file"; 

     // Create a File object that points to the file we input in the File Name 
     QFile mFile("/home/mohamed/Desktop/index.dat"); 
     QFile readFile(str); 

      // Check if there is a problem 
      if(!mFile.open(QFile::WriteOnly | QFile::Append)){ 
       qDebug() << "Could not open the file for writing"; 
       return; 
      } 




      QTextStream in(&readFile); 
      if(!readFile.open(QFile::ReadOnly | QFile::Text)){ 
       qDebug() << "Could not open the file for writing"; 
       return; 
      } 
      // a text stream object that will print in the the file 
      QTextStream out(&mFile); 

      QString mText = in.readAll(); 
      mText.replace("de","BURGER"); 
      ui->txtSearchResult->setText(mText); 
      out << mText; 

///home/mohamed/NetBeansProjects/indexing/fichier.txt 


      mFile.flush(); 

      readFile.close(); 
      mFile.close(); 


    } 

    else{ 
     qDebug() << " A FOLDER"; 
    } 

であり、ここで正常に動作します私の通常のC++のコードです!

#include <cstdlib> 
#include <iostream> 
#include <fstream> 
#include<cstdlib> 
#include <dirent.h> 
using namespace std; 

bool searchArray(string s, string undesired[]){ 
for(int i=0; i<50;i++){ 
    if(undesired[i] == s){ 
     return true; 
     break; 
    } 
} 
    return false; 
} 
int main(int argc, char** argv) { 
// array filled with undesired words 
string undesired[50] = {"je","tu","il","nous","faut","faire","de","ce","se"}; 
// the directory 


// the file to read from 
ifstream reader("fichier"); 
// the file to write to 
ofstream writer("index.dat", ios_base::app); 
string word, message =" "; 
int count = 0; 


while(!reader.eof()){ // while there still is stuff to read 
    reader >> word; // read one word 

    if(!searchArray(word,undesired)){ // if it's an undesired word 

     writer << word <<"\t\t\t\t" <<count<<endl;// print it in the text 

     cout<<"word inserted"<<endl; 
    } 
    count++; 
     } 






return 0; 

}

+0

あなたはどのようなエラーがありましたか? –

答えて

1

あなたの目的を達成するためにQString::splitQStringList::joinを組み合わせることができます

#include <QFile> 
#include <QStringList> 
#include <QCoreApplication> 
#include <QTextStream> 

int main() 
{ 
    QFile ifile("in.txt"); 
    ifile.open(QIODevice::ReadOnly | QIODevice::Text); 

    // read whole content 
    QString content = ifile.readAll(); 
    // extract words 
    QStringList list = content.split(" "); 

    ifile.close(); 

    QFile ofile("out.txt"); 
    ofile.open(QIODevice::WriteOnly | QIODevice::Text); 

    QTextStream out(&ofile); 

    // join QStringList by "\n" to write each single word in an own line 
    out << list.join("\n"); 

    ofile.close(); 

    return 0; 
} 
+0

ありがとう、バンチマン!魅力のように働いた。 –

関連する問題