eof isn't working

This is a discussion on eof isn't working within the Windows Programming forums, part of the Platform Specific Boards category; Code: // Fill Refunds list while(1){ ifstream ifs; char filedat0[50]="

Thread: eof isn't working

"; // File data buffer char pzData[50]="

Thread: eof isn't working

"; // Refund data ifs.open("C:\Samudracoldb\Refunds.txt",ios::in); ...

  1. #1
    Android
    Join Date
    Mar 2004
    Location
    Kurunegala Colony, Sri Lanka, Sri Lanka
    Posts
    362

    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.
    Some useless programs i've wrote : slprogrammersheaven
    Youtube: http://www.youtube.com/user/geekyzine
    My amature IT Blog: http://everything-geeky.blogspot.com/
    My Flash games: http://flashweed.blogspot.com/

    (and, sorry for my amature english)

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,498
    > 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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Android
    Join Date
    Mar 2004
    Location
    Kurunegala Colony, Sri Lanka, Sri Lanka
    Posts
    362
    i dont get buffer overflow errors (cos every feild contains less than 7 chars - except the name field). but eof() never returns true ?
    Some useless programs i've wrote : slprogrammersheaven
    Youtube: http://www.youtube.com/user/geekyzine
    My amature IT Blog: http://everything-geeky.blogspot.com/
    My Flash games: http://flashweed.blogspot.com/

    (and, sorry for my amature english)

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,498
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  5. #5
    Android
    Join Date
    Mar 2004
    Location
    Kurunegala Colony, Sri Lanka, Sri Lanka
    Posts
    362
    >why are you opening the file INSIDE the while loop?
    sorry my mistake -- open() should be on outside the loop. thx.
    Some useless programs i've wrote : slprogrammersheaven
    Youtube: http://www.youtube.com/user/geekyzine
    My amature IT Blog: http://everything-geeky.blogspot.com/
    My Flash games: http://flashweed.blogspot.com/

    (and, sorry for my amature 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, 08: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