avoiding a problem with a blank line at the end of the file?

This is a discussion on avoiding a problem with a blank line at the end of the file? within the C++ Programming forums, part of the General Programming Boards category; Hi, I have many times experienced the following problem. I am reading from a file like this: Code: while(!SomeFile.eof()) { ...

  1. #1
    Registered User MathFan's Avatar
    Join Date
    Apr 2002
    Posts
    190

    avoiding a problem with a blank line at the end of the file?

    Hi,

    I have many times experienced the following problem. I am reading from a file like this:

    Code:
    while(!SomeFile.eof())
    	{
    		SomFile>>Param;
    	}
    Now if the file SomeFile contains a blank line at its end (or several of those), and we have read the last number (or whatever) from it, the SomeFile.eof() will not report EOF, because there is still one or more blank lines left. But when in the next loop it tries to read from the file, it fails, because there is no values left in the file.

    Is there any smart way to avoid this problem?

    It's not like it matters much, but it gets on my nerves each time my programme crashes just out of the blue and I can't find what's the matter. And then, after 20 minutes of debugging, I finally take a look at the input file to find out that, "Dah... I left a blank line at the end of the file...." Kind of annoying hehe

    Thanks a lot!
    The OS requirements were Windows Vista Ultimate or better, so we used Linux.

  2. #2
    Senior Member joshdick's Avatar
    Join Date
    Nov 2002
    Location
    Phildelphia, PA
    Posts
    1,146
    Code:
    while( SomeFile >> Param )
    	{
    		cout << Param;
    	}
    Try that.
    FAQ

    "The computer programmer is a creator of universes for which he alone is responsible. Universes of virtually unlimited complexity can be created in the form of computer programs." -- Joseph Weizenbaum.

    "If you cannot grok the overall structure of a program while taking a shower, you are not ready to code it." -- Richard Pattis.

  3. #3
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    http://faq.cprogramming.com/cgi-bin/...&id=1043284351

    Same reasoning applies to using the eof() function in a stream

  4. #4
    Registered User MathFan's Avatar
    Join Date
    Apr 2002
    Posts
    190
    That helped a lot. Thanks for the help!
    The OS requirements were Windows Vista Ultimate or better, so we used Linux.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Newbie homework help
    By fossage in forum C Programming
    Replies: 3
    Last Post: 04-30-2009, 05:27 PM
  2. File transfer- the file sometimes not full transferred
    By shu_fei86 in forum C# Programming
    Replies: 13
    Last Post: 03-13-2009, 01:44 PM
  3. opening empty file causes access violation
    By trevordunstan in forum C Programming
    Replies: 10
    Last Post: 10-22-2008, 12:19 AM
  4. Inventory records
    By jsbeckton in forum C Programming
    Replies: 23
    Last Post: 06-28-2007, 05:14 AM

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