即时通讯存在的问题是,我能够创建文本文件,写入文本一次。我希望能够在需要时添加更多行,而无需创建新文件。我的下一个问题是,我似乎无法得到即时消息寻找的输出。例如。文本文件包含如何从C++中的txt文件检索数据
启动1
FNAME | L-NAME | SSN
END1
我的目标是只得到启动1和END1内的数据并返回FNAME LNAME和SSN无定界符|。这里是我的代码
int main()
{
fstream filestr;
string line;
filestr.open ("file.txt", fstream::in | fstream::out | fstream::app);
if(!filestr.is_open())
{
cout << "Input file connection failed.\n";
exit(1);
}
else{
filestr<<"Start2\n";
filestr<< "middle|middle"<<endl;
filestr<<"end2"<<endl;
if(filestr.good()){
while(getline(filestr, line) && line !="end1"){
if(line !="Start1"){
//below this point the output goes screwy
while(getline(filestr, line,'|')){
cout<<"\n"<<line;
}
}
}
}
filestr.close();
}
作为洛基阿斯塔在提到下面的文章,filestr.seekg(0);在循环之前需要。 – Jagannath