Thread: eof isn't working

  1. #1
    Android geek@02's Avatar
    Join Date
    Mar 2004
    Location
    Kurunegala Colony, Sri Lanka, Sri Lanka
    Posts
    470

    eof isn't working

    Code:
    // Fill Refunds list
        while(1){
            ifstream ifs;
            char filedat0[50]="\0"; // File data buffer
            char pzData[50]="\0"; // Refund data
            ifs.open("C:\\Samudracoldb\\Refunds.txt",ios::in);
            ifs.getline(filedat0,30,'\n'); // Skip the 1st empty line
            ifs.getline(filedat0,30,'\n'); // Get index
            strcat(pzData,filedat0); // Append file data
            strcat(pzData," "); // Put a space
            ifs.getline(filedat0,30,'\n'); // Get name
            strcat(pzData,filedat0);
            strcat(pzData," ");
            ifs.getline(filedat0,30,'\n'); // Get item
            strcat(pzData,filedat0);
            strcat(pzData," ");
            ifs.getline(filedat0,30,'\n'); // Pass item n
            ifs.getline(filedat0,30,'\n'); // Pass price
            ifs.getline(filedat0,30,'\n'); // Get total
            strcat(pzData,filedat0);
            strcat(pzData,"/=");
            ifs.getline(filedat0,30,'\n'); // Skip last empty line
    
            m_clistRefunds.AddString(pzData);
            
            if(ifs.eof()) //*
                break; // Break at file end
        }
    Hi
    eof() isn't working here. the while loop never breaks. whats the problem here please? thanx.
    GameJolt: https://gamejolt.com/@KasunL
    Game Development Youtube:
    https://is.gd/XyhYoP
    Amateur IT Blog: http://everything-geeky.blogspot.com/



    (and, sorry for my amateur English)

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > char pzData[50]="\0"; // Refund data
    Just how many fields of up to 30 characters do you expect to be able to add to this array of 50 chars?

    Stop messing with small char arrays which you're not using in any safe manner, and start using std::string.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Android geek@02's Avatar
    Join Date
    Mar 2004
    Location
    Kurunegala Colony, Sri Lanka, Sri Lanka
    Posts
    470
    i dont get buffer overflow errors (cos every feild contains less than 7 chars - except the name field). but eof() never returns true ?
    GameJolt: https://gamejolt.com/@KasunL
    Game Development Youtube:
    https://is.gd/XyhYoP
    Amateur IT Blog: http://everything-geeky.blogspot.com/



    (and, sorry for my amateur English)

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Including the \0 and the 6 other characters you concatenate as well?

    Also, why are you opening the file INSIDE the while loop?

    Also, eof() only becomes true when an input fails.
    > ifs.getline(filedat0,30,'\n'); // Skip last empty line
    Assuming this reads the last line, it will leave the file pointer at the end of the file, but it will NOT make eof() true. For that, you would need ANOTHER getline() call, which would have to fail to read any input, before eof() would become true.

    eof() is a STATE set by a past event, not a prediction for a future event.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Android geek@02's Avatar
    Join Date
    Mar 2004
    Location
    Kurunegala Colony, Sri Lanka, Sri Lanka
    Posts
    470
    >why are you opening the file INSIDE the while loop?
    sorry my mistake -- open() should be on outside the loop. thx.
    GameJolt: https://gamejolt.com/@KasunL
    Game Development Youtube:
    https://is.gd/XyhYoP
    Amateur IT Blog: http://everything-geeky.blogspot.com/



    (and, sorry for my amateur English)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Check for EOF when using fgets
    By daghenningsorbo in forum C Programming
    Replies: 6
    Last Post: 05-16-2007, 06:48 PM
  2. EOF isnt working....
    By i_can_do_this in forum C Programming
    Replies: 2
    Last Post: 07-10-2006, 09:58 AM
  3. EOF messing up my input stream?
    By Decrypt in forum C++ Programming
    Replies: 4
    Last Post: 09-30-2005, 03:00 PM
  4. Replies: 2
    Last Post: 11-10-2003, 09:12 PM
  5. files won't stop being read!!!
    By jverkoey in forum C++ Programming
    Replies: 15
    Last Post: 04-10-2003, 05:28 AM