【发布时间】:2016-04-24 08:50:39
【问题描述】:
一切顺利,直到 f
unsigned int temp_int = 0;
fstream f("resources/saveData/Player/savelog.txt");
if (!f)
{
cout << "error accessing savelist" << endl;
}
else
{
string skip;
std::stringstream iss;
string line;
readVarFromFile(f, iss, skip, line, { &temp_int }); //check how many saves currently
temp_int += 1; //increment number of saves by 1
f.seekp(ios_base::beg);
cout << "Write position: " << f.tellp() << endl; //check stream is at beginning
f << "<NumberSaves>" << temp_int << endl; //truncate <NumberSaves> 'x' with <NumberSaves> 'x + 1'
cout << "Write position: " << f.tellp() << endl; //position suggests the entire string has been written, only two characters have been
if (!f)
{
cout << "ERROR";
}
f.seekp(ios_base::end);
f << currentPlayer->getName(); //append players name to end of file
}
想要的输出如下
数字保存 2
播放器
另一个玩家
电流输出
努
播放器
【问题讨论】:
-
什么是
readVarFromFile()?请提供minimal reproducible example。 -
意识到寻找文件中的某个位置最适合以二进制模式打开的文件。如果文件以文本模式打开,使用
seekp、tellp等将无法获得所需的结果,因为正在进行换行和eof 转换。 -
这是我做的一个功能,不是问题的一部分,如果造成混乱,对不起