Thread: Problem opening blank files with ifstream.

  1. #1
    Registered User
    Join Date
    Jan 2009
    Posts
    14

    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
    The larch
    Join Date
    May 2006
    Posts
    3,573
    I think the problem is with controlling input loop with eof (see FAQ why you shouldn't do that). See, before the loop eof is not true. The first input will be performed anyway.

    Another option is that there is a blank line in the file (are you sure the size of the file is 0 bytes?).
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  3. #3
    Registered User
    Join Date
    Jan 2009
    Posts
    14
    You were right, thanks anon!

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

  4. #4
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Peeking is not the standard way to do it either. Input functions themselves report back whether input was successful (or more precisely, whether the input stream is in a good state), so they can be used to control looping directly:

    Code:
    while (geline(myStream, string))...
    Your code, as it is, doesn't try to avoid going out of bounds either. What if there happens to be more than 4 lines in the file?
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  5. #5
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    Code:
    std::vector<std::string> stringVector;
    std::string lineFromFile;
    std::ifstream inFile("somefile.txt");
    
    while (getline(inFile,lineFromFile))
    {
       stringVector.push_back(lineFromFile);
    }
    
    inFile.close();
    You could possibly blow size_t but that would be a very large file.
    Last edited by VirtualAce; 02-07-2009 at 11:46 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem with deleting completely blank lines
    By dnguyen1022 in forum C Programming
    Replies: 3
    Last Post: 12-07-2008, 11:51 AM
  2. Replies: 3
    Last Post: 04-27-2005, 11:50 AM
  3. problem while opening files from multiple directories
    By V.G in forum Windows Programming
    Replies: 2
    Last Post: 11-08-2004, 03:29 PM
  4. problem with sending files to a class method..
    By utoots in forum C++ Programming
    Replies: 5
    Last Post: 04-02-2003, 01:38 AM
  5. ifstream problem
    By AzraelKans in forum C++ Programming
    Replies: 2
    Last Post: 10-28-2002, 03:10 PM