everthing进行得很顺利,直到f < <“string”< < temp_int < < endl;声明 得到不同openmodes不同的结果,无论是在所有不写或写“NumberSaves”fstream <<操作不写入整个输出
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
}
所需的输出如下
NumberSaves的前两个字符2
球员
anotherplayer
电流输出
怒江
球员
什么是'readVarFromFile()'?请提供[MCVE]。 –
认识到寻找文件中的位置最适合以二进制模式打开的文件。如果文件以文本模式打开,则由于执行了新行和eof翻译,您将无法使用“seekp”,“tellp”等获得所需的结果。 – PaulMcKenzie
这是一个功能,我所做的不是问题的一部分,对不起,如果它造成混淆 –