我有一個txt文件是這樣的:如何使用C++編輯文本文件中的一行?
"shoes":12
"pants":33
"jacket":26
"glasses":16
"t-shirt":182
我需要更換套的數量(從26至42例如)。所以,我寫了這個代碼,但我不知道如何編輯特定的行那裏是單詞「外套」:
所有的#include <iostream>
#include <fstream>
using namespace std;
int main() {
ifstream f("file.txt");
string s;
if(!f) {
cout< <"file does not exist!";
return -1;
}
while(f.good())
{
getline(f, s);
// if there is the "jacket" in this row, then replace 26 with 42.
}
f.close();
return 0;
}
這是C++,所以它不是重複的。 – xRobot 2012-04-19 10:09:28
你想在C,C++還是兩者兼顧的解決方案? – 2012-04-19 10:11:32
在C或C++中......無所謂:) – xRobot 2012-04-19 10:12:32