[RESOLVED] Problem opening blank files with ifstream
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4

Thread: [RESOLVED] Problem opening blank files with ifstream

  1. #1
    Join Date
    Jan 2009
    Posts
    13

    [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!

  2. #2
    Join Date
    Jan 2009
    Posts
    13

    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;
    }

  3. #3
    Join Date
    Aug 2000
    Location
    West Virginia
    Posts
    7,565

    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
    }

  4. #4
    Join Date
    Jan 2009
    Posts
    13

    Re: Problem opening blank files with ifstream

    Quote Originally Posted by Philip Nicoletti View Post
    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
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center