I'm having problems opening blank files with ifstream. It seems to be saving a blank line over my first string variable. For example I created a completely blank text file in notepad and saved it as SAVEGAME.DAT. I then try to open it with the following code.
This gives the the output of:Code:#include <cstdlib> #include <iostream> #include <string> #include <fstream> using namespace std; int main() { int a = 0; string tempString[3]; tempString[0] = "empty"; tempString[1] = "empty"; tempString[2] = "empty"; ifstream myStream("SAVEGAME.DAT"); while(!myStream.eof()) { getline(myStream, tempString[a]); a++; } cout << tempString[0] << endl; cout << tempString[1] << endl; cout << tempString[2] << endl; system("PAUSE"); return 0; }
So somehow its saving over my first string variable even though the SAVEGAME.DAT file is completely blankCode:1. [first line is blank] 2. empty 3. empty 4. Press any key to continue.
It works fine however if SAVEGAME.DAT has a line of text in it. When I open SAVEGAME.DAT and insert and save some text. For example if I save the word "sometext" in SAVEGAME.DAT. I get the following:
This is working fine^ It only doesn't work right if the file is blank.Code:1. sometext 2. empty 3. empty 4. Press any key to continue.
Thank you!



LinkBack URL
About LinkBacks


