4

我想知道如何将文本文件逐块拆分,并将行组存储为单个字符串。例如:

我有一个问题文本文件,其中一些是多行的。在可变数量的行之后(取决于问题占用的行数)有一个空白行,然后是答案,然后是另一个问题(也可能比 1 行长),空白行,答案。

像这样的东西,其中“q”是应该存储为单个字符串的行,而“a”也应该是单个字符串:

qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq

aaaaaaaaaaaaaaaaaaaaaaa

qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq

aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa

我尝试逐行阅读,结合string line + lineif line != ""。但它变得混乱和混乱,我无法让它正常工作。

我只是想将第一组q's 存储为单个字符串并将其放入,vector[0]并将第一组a's 放入vector[1]. 第二组q's in vector[2]. 第二组a's in vector[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;

}

4

3 回答 3

3

我稍微调整了代码并让它工作!

我改变了循环遍历文本文件行的方式,将其更改为 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;

}
于 2013-02-07T00:36:19.083 回答
1

如果您使用的是 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'
}

您将需要包含QFileQStringQTextStream才能QStringList使其正常工作。祝你好运!

于 2013-02-05T20:58:03.583 回答
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);
}
于 2013-02-05T20:58:14.277 回答