如果我的文本文件看起來如此..如何使用C++在特定位置將數據從矢量寫入文本文件?
[email protected] 3223434324數據1數據2
[email protected] 2343454454數據3數據4
我一直在使用矢量讀取這些值。 我需要在文件第一行的末尾添加一個字符串。 我該怎麼辦? 我的代碼如下:
system("cls");
cout << "\nView proposal\n";
fstream file1;
string ap, aname, aphno;
file1.open("C:\\Users\\Nandhu\\Desktop\\glosys\\joinpolicy.txt",
ios::in|ios::app);
while(!file1.eof())
{
while(getline(file1, text))
{
istringstream xstream(text);
vector<string> vcol;
while(getline(xstream, word, ','))
{
vcol.push_back(word);
}
vec.push_back(vcol);
}
file1.close();
}
for(vector<vector<string> >::iterator itr = vec.begin();
itr != vec.end(); itr++)
{
for(vector<string>::iterator itr2 = itr->begin();
itr2 != itr->end(); itr2++)
{
string str;
str = *itr2;
cout << str << " ";
}
cout << "\n";
}
cout << "\n1.Approve";
cout << "\n2.Reject";
cout << "\nDo you wish to approve?";
cin >> ans;
if(ans == 'y')
{
file1.open("C:\\Users\\Nandhu\\Desktop\\glosys\\joinpolicy.txt",
ios::in|ios::app);
file1.seekg(0);
cout << "\nEnter the customer id you wish to approve for:";
cin >> aname;
vec.clear();
if(file1.is_open())
{
while(getline(file1, text))
{
istringstream xstream(text);
vector<string> vcol;
while(getline(xstream, word, ','))
{
vcol.push_back(word);
}
vec.push_back(vcol);
}
file1.close();
}
for(vector<vector<string> >::iterator itr = vec.begin();
itr != vec.end(); itr++)
{
vector<vector<string> > vec;
vector<string> vcol;
for(vector<string>::iterator itr2 = itr->begin();
itr2 != itr->end(); itr2++)
{
string str;
str=*itr2;
if(str==aname)
{
itr + 3;
file1.open("C:\\Users\\Nandhu\\Desktop\\glosys\\joinpolicy.txt",
ios::out|ios::app);
vector<string>.insert(itr->end(), "approved");
file1 << "approved";
cout << "approved";
file1.close();
}
}
}
}
break;
似乎缺少一些代碼和格式是遍佈店鋪請更正 –
請修復格式和縮進代碼 –
我的代碼現在好嗎? – nandhu