0
我想從文件中讀取某些內容,然後寫入同一個文件。 我有這樣的事情,並創造了奇蹟:試圖讀取然後寫入CSV文件
int main() {
fstream fp;
string filePath = "test.csv";
string outString;
outString += "g1;1901;3;3;4;3;3;3;3\n";
outString += ";1902;3;3;4;3;3;3;3\n";
fp.open(filePath, ios::app | ios::in | ios::out);
fp << outString;
fp.close();
return 0;
}
然後我讀的一部分,它讀取很好,但是之後它不寫:如果它不」
int main() {
fstream fp;
string outString;
string filePath = "test.csv";
string line, csvItem;
int numberOfGames = 0;
fp.open(filePath, ios::app | ios::in | ios::out);
if (fp.is_open()) {
while (getline(fp, line)) {
istringstream myline(line);
while (getline(myline, csvItem, ';')) {
cout << csvItem << endl;
if (csvItem != "" && csvItem.at(0) == 'g') {
numberOfGames++;
}
else
break;
}
}
}
if (numberOfGames == 0) {
outString = "Game;Year;AUS;ENG;FRA;GER;ITA;RUS;TUR\n";
}
outString += "g";
outString += to_string(numberOfGames + 1);
outString += ";1901;3;3;4;3;3;3;3\n";
outString += ";1902;3;3;4;3;3;3;3\n";
fp << outString;
fp.close();
return 0;
}
該文件被創建否則,它被讀取。 閱讀部分基於這個問題的接受答案: Reading a particular line in a csv file in C++。
非常感謝!
謝謝SOOOO非常感謝! :d –