EOF woes - split from File I/O

This is a discussion on EOF woes - split from File I/O within the C++ Programming forums, part of the General Programming Boards category; I dont mean to hijack the thread but maybe you could explain my problem as it seems to be on ...

  1. #1
    Registered User
    Join Date
    Jan 2007
    Posts
    5
    I dont mean to hijack the thread but maybe you could explain my problem as it seems to be on the same lines as jedi's. Basically i have written to a file and now im trying to read it back but when i do, i hit an infinite loop and the code breaks!!

    Code:
    void inClientForm(customer* theRecord, int size)
    {
    	char custname[20];
    	int age, purchaseCount;
    	char gender[10];
    	float purchaseCost;
    
    	ifstream inClientFile( "clients.txt", ios::in );
    
    	while(!inClientFile.eof())
    	{
    		inClientFile.getline(custname,20);
    		if (!inClientFile.eof())
            cout << custname << endl;
    		inClientFile >> age;
    		if (!inClientFile.eof())
    		 cout << age << endl;
    		inClientFile.getline(gender,10);
    		if (!inClientFile.eof())
    		 cout << gender << endl;
    		inClientFile >> purchaseCount;
    		if (!inClientFile.eof())
    		 cout << purchaseCount << endl;
    		inClientFile >> purchaseCost;
    		if (!inClientFile.eof())
    		cout << purchaseCost << endl;
    	}
    }
    I have put the IF statements before each print to try and solve the problem or atleast find where its breaking but to no luck!!

  2. #2
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    don't use eof to control the loop
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  3. #3
    Registered User
    Join Date
    Jan 2005
    Posts
    7,328
    >> I dont mean to hijack the thread but maybe you could explain my problem as it seems to be on the same lines as jedi's.
    Please start a new thread and link to this one if you think it is related. Perhaps a moderator will separate your question for you. If so, this response can come along.

    An infinite loop means an error has occurred while reading before the eof has been reached. Since your code does not check for read errors, it goes on forever attempting to read but failing because the stream is in a fail state. You need to check for successful read as 7stud pointed out. Simply using "while (inClientFile)" or "if (!inClientFile) then error out" should be good enough. You will still have to figure out why an error is occurring during the read.

    My guess on why the error is occuring during the read is your mix of getline and operator>>. The getline function reads until it finds a newline character. The operator>> leaves any trailing whitespace in the input stream, including trailing newlines. At the end of your loop you read purchaseCost with operator>>. If there is a new line after the purchaseCost in the input file, then it will remain in the stream until the next time through the loop. The call to getline will then hit that newline and stop, leaving the custname empty and leaving the actual name in the input file stream. When you then attempt to read the age, it finds the name still there and fails because you cannot read a character into an int.

    To solve that, add a inClientFile.ignore() after you read in purchaseCost (or after all calls to operator>> but not after getline). This will ignore trailing whitespace which will probably allow your getline to read in the next name properly.

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,558
    Split from File I/O questions
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A development process
    By Noir in forum C Programming
    Replies: 37
    Last Post: 07-10-2011, 10:39 PM
  2. File transfer- the file sometimes not full transferred
    By shu_fei86 in forum C# Programming
    Replies: 13
    Last Post: 03-13-2009, 12:44 PM
  3. Need Help Fixing My C Program. Deals with File I/O
    By Matus in forum C Programming
    Replies: 7
    Last Post: 04-29-2008, 07:51 PM
  4. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  5. advice on file i/o
    By Unregistered in forum C Programming
    Replies: 1
    Last Post: 11-29-2001, 04:56 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21