編輯:改變我的問題,以更準確的情況編輯:麻煩檢查文件是否爲空,我做錯了什麼?
我想打開一個文本文件(創建它,如果它不存在,打開它,如果沒有)。它與輸出是相同的輸入文件。
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循環,通常會要求我設置一個新目標。我似乎無法弄清楚這裏發生了什麼。
嘆息.. 爲什麼downvotes? – Robolisk
尋求到最後,並獲得位置。如果它是零,那麼文件是空的。 –
是的,但爲什麼我沒有工作,爲什麼發生了什麼,發生了什麼? – Robolisk