如何解析带有换行符的文件作为c ++中的分隔符?

时间:2013-02-05 20:49:32

标签: c++ parsing newline line-breaks delimiter

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

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

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

qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq

aaaaaaaaaaaaaaaaaaaaaaa

qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq

aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa

我尝试逐行阅读,如果string line + line合并line != ""。但它让人感到困惑和混乱,我无法让它正常工作。

我只想将第一组q存储为单个字符串并将其放在vector[0]中,将a的第一组存储在vector[1]中。 q中的第二组vector[2]a中的第二组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;

}

3 个答案:

答案 0 :(得分: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;

}

答案 1 :(得分: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才能生效。祝你好运!

答案 2 :(得分: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);
}