【发布时间】:2016-03-06 03:57:05
【问题描述】:
编辑:将我的问题更改为更准确的情况
我正在尝试打开一个文本文件(如果它不存在则创建它,如果它不存在则打开它)。它是与输出相同的输入文件。
ofstream oFile("goalsFile.txt");
fstream iFile("goalsFile.txt");
string goalsText;
string tempBuffer;
//int fileLength = 0;
bool empty = false;
if (oFile.is_open())
{
if (iFile.is_open())
{
iFile >> tempBuffer;
iFile.seekg(0, iFile.end);
size_t fileLength = iFile.tellg();
iFile.seekg(0, iFile.beg);
if (fileLength == 0)
{
cout << "Set a new goal\n" << "Goal Name:"; //if I end debugging her the file ends up being empty
getline(cin, goalSet);
oFile << goalSet;
oFile << ";";
cout << endl;
cout << "Goal Cost:";
getline(cin, tempBuffer);
goalCost = stoi(tempBuffer);
oFile << goalCost;
cout << endl;
}
}
}
几个问题。一方面,如果文件存在并且其中有文本,它仍然会进入通常会要求我设定新目标的 if 循环。我似乎无法弄清楚这里发生了什么。
【问题讨论】:
-
叹息.. 为什么投反对票?
-
追到底,拿到位置。如果为零,则文件为空。
-
是的,但是为什么我的工作没有工作,为什么发生了什么,发生了什么?。
-
@JoachimPileborg 我尝试使用我所理解的 seek 到最后,但它仍然不起作用,有什么提示吗?
标签: c++ fileinputstream