2013-02-05 46 views
2

我想知道如何去分裂一個文本文件,塊大塊和存儲線組作爲單個字符串..例如:如何用c換行符作爲分隔符來解析文件?

我有一個問題的文本文件,其中一些是多個線。在可變數量的行之後(取決於問題佔用的行數),有一個空白行,然後是答案,然後是另一個問題(也可能長於1行),空行,答案。

這樣的事情,其中​​「Q」是應該被存儲爲單個串和「a」也應該是一個字符串行:

qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq 

aaaaaaaaaaaaaaaaaaaaaaa 

qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq 

aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa 

我試圖讀取一行行,如果組合string line + lineline != ""。但它令人困惑和混亂,我無法讓它正常工作。

我只是想存儲第一組q的一個字符串,並把它放在vector[0]和第一組的a「在vector[1]秒。第二組qvector[2]。第二套avector[3] ..等等。 q和a都可以是幾行。


任何建議或幫助將不勝感激!先謝謝了!

#include <vector> 
#include <fstream> 
#include <iostream> 
#include "Question.h" 
#include <iomanip> 

using namespace std; 

int main(int argc, char * argv[]){ 
    ifstream infile; 
    string filename = "questions.txt";//manually set for testing. 

    //cout<<"Enter the questions file: "; 
    //cin>>filename; 

    infile.open(filename.c_str()); 

    if (!infile){ 
      cout<<"error"<<endl; 
      return 0; 
    } 
    else { 
      cout<<"file opened!"<<endl; 
    } 
    vector<string> myvector; 

    string line; 
    string additionalLine; 
    int totalLines = 0; 
    while(getline(infile,line)){ 
      totalLines++; 
    } 
    cout<<"total lines: "<<totalLines<<endl; 

/* 
    while(getline(infile,line,'\n')){ 
      cout<<line<<endl; 
    } 
*/ 

    while(getline(infile,line,\n)){ 
      if (line == ""){ 
        cout<<"empty"<<endl; 
      } 

      else { 
        cout<<"line is not empty"<<endl; 
        additionalLine = additionalLine + line; 
      } 
      if (line != ""){ 
        myvector.push_back(additionalLine); 
      } 

    } 

    for(int i=0; i < (myvector.size()); i++){ 
      cout<<myvector[i]<<endl; 
    } 

    //TESTING 
    cout<<"Question: "<<endl; 
    cout<<myvector[0]<<endl; 
    cout<<"Answer: "<<endl; 
    cout<<myvector[1]<<endl; 

    return 0; 

}

+2

你可以發佈你到目前爲止的代碼嗎? – hmjd

+0

是的,我會發布它。我一直在搞這個,現在看起來像是無稽之談。 – ModdedLife

回答

1

我調整了一下代碼,並讓它工作!

我改變了我通過文本文件的行進行循環的方式,將其更改爲while(!infile.eof())並手動獲取行。我還添加了一個聲明來檢測是否有新行。我還必須重新定位將字符串變量重置爲「」的位置。

感謝您的建議!繼承人的解決方案代碼:

#include <vector> 
#include <fstream> 
#include <iostream> 
#include "Question.h" 
#include <iomanip> 

using namespace std; 

int main(int argc, char * argv[]){ 
    ifstream infile; 
    string filename = "questions.txt";//manually set for testing. 

    //cout<<"Enter the questions file: "; 
    //cin>>filename; 

    infile.open(filename.c_str()); 

    if (!infile){ 
      cout<<"error"<<endl; 
      return 0; 
    } 
    else { 
      cout<<"file opened!"<<endl; 
    } 
    vector<string> myVec; 

    string line; 
    string comboLine=""; 

    while(!infile.eof()){ 
      getline(infile,line); 

      if (line == "" || line == "\0") { 
        //cout<<"->BLANK LINE DETECTED<-"<<endl; 
        myVec.push_back(comboLine); 
        comboLine=""; 
      }else { 
        comboLine = comboLine + line; 
        //cout<<"comb: "<<comboLine<<endl; 
      } 

      line = ""; 

    } 
    infile.close(); 

    //TESTING 
    cout<<"Question: "<<endl; 
    cout<<myVec[0]<<endl; 
    cout<<"Answer: "<<endl; 
    cout<<myVec[1]<<endl; 
    cout<<"Question 2: "<<endl<<myVec[2]<<endl; 
    cout<<"Answer 2: "<<endl<<myVec[3]<<endl; 
    return 0; 

} 
0

如果你使用Qt,您可以輕鬆地做到這一點。

QFile myFile; 
myFile.open(QIODevice::ReadOnly | QIODevice::Text); 

QTextStream openFile(&myFile); 

while (!myFile.atEnd()) { 

    QString line; 
    QStringList tokens; 
    line = myFile.readLine(); 
    tokens = line.split("\n"); 

    // additional processing here with your tokens, which are delimited by '\n' 
} 

您將需要包括QFileQStringQTextStreamQStringList這個工作。祝你好運!

+1

如果可能,我寧願直C++。但如果一切都失敗了,我一定會嘗試。謝謝! – ModdedLife

0
struct QA { 
    vector<string> questionLines; 
    vector<string> answerLines; 
}; 

vector<string> getLines(istream& is) { 
    vector<string> lines; 
    string line; 
    do { 
     getline(is, line); 
     lines.push_back(line); 
    } while(!line.empty()) 
    return lines; 
} 

istream operator>>(istream& is, QA& qa) { 
    qa.questionLines = getLines(is); 
    qa.answerLines = getLines(is); 
} 
相關問題