テキストファイルをチャンクごとに分割し、行のグループを単一の文字列として保存する方法を考えていました..例:
質問のテキスト ファイルがありますが、その中には複数行あるものもあります。可変数の行 (質問が占める行数に応じて) の後に、空白行があり、次に回答があり、その後に別の質問 (1 行より長くなる場合もあります)、空白行、回答が続きます。
このようなもので、「q」は単一の文字列として保存する必要がある行であり、「a」も単一の文字列である必要があります。
qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq
aaaaaaaaaaaaaaaaaaaaaaa
qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
string line + line
ifを組み合わせて、1行ずつ読んでみline != ""
ました。しかし、それは混乱して面倒になり、正しく動作させることができませんでした.
q
の最初のセットを1 つの文字列として保存し、それを に入れvector[0]
、最初のa
のセットを に入れたいだけですvector[1]
。q
の の 2 番目のセットvector[2]
。の 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;
}