-
February 7th, 2009, 06:09 AM
#1
[RESOLVED] Problem opening blank files with ifstream
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.
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;
}
This gives the the output of:
Code:
1. [first line is blank]
2. empty
3. empty
4. Press any key to continue.
So somehow its saving over my first string variable even though the SAVEGAME.DAT file is completely blank
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:
Code:
1. sometext
2. empty
3. empty
4. Press any key to continue.
This is working fine^ It only doesn't work right if the file is blank.
Thank you!
-
February 7th, 2009, 10:15 AM
#2
Re: Problem opening blank files with ifstream
I figured it out. Here is the updated code:
Here is updated code that works great for my save game function in my game just in case anyone has similar problems to this in the future and stumbles upon this on the google or something
Code:
#include <string>
#include <iostream>
#include <cstdlib>
#include <fstream>
using namespace std;
int main()
{
int tmpInt =0;
string tmpString;
string stringArray[4];
stringArray[0] = "empty";
stringArray[1] = "empty";
stringArray[2] = "empty";
stringArray[3] = "empty";
ifstream myStream("SAVEGAME.DAT");
while(!(myStream.peek() == EOF))
{
getline(myStream, tmpString);
stringArray[tmpInt] = tmpString;
tmpInt++;
}
for (tmpInt = 0; tmpInt < 4; tmpInt++)
{
cout << stringArray[tmpInt] << endl;
}
myStream.clear();
myStream.close();
system("PAUSE");
return 0;
}
-
February 7th, 2009, 12:03 PM
#3
Re: Problem opening blank files with ifstream
As you discovered, eof() does not become true untill you actually
attempt a read past the eof.
The normal way is to put the read in the "while" statement (much cleaner
than the peek()) ...
Code:
while (getline(myStream, tempString[a]))
{
//process the line
}
-
February 9th, 2009, 06:08 AM
#4
Re: Problem opening blank files with ifstream
Originally Posted by Philip Nicoletti
As you discovered, eof() does not become true untill you actually
attempt a read past the eof.
The normal way is to put the read in the "while" statement (much cleaner
than the peek()) ...
Code:
while (getline(myStream, tempString[a]))
{
//process the line
}
Thanks
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|