Thread: Discarding blank lines?

  1. #1
    Registered User
    Join Date
    Jul 2007
    Posts
    15

    Discarding blank lines?

    Hey,

    I'm writing a program that takes input from a file called index.txt. I want to be able to discard blank lines. This is what I have:

    Code:
           while (!feof(fp_fileNamesCheck))
              {
                char buf[256];
                fgets(buf, 255, fp_fileNamesCheck);
                if (strlen(buf) > 1)
                  newNamesCount++;
              }
    This works when I have many blank lines in the document, but it doesn't catch trailing blank lines.

    Ex, it catches

    |File1
    |File2
    |
    |
    |

    but doesn't catch

    |File1
    |File2
    |

    Any help would be appreciated. Thanks.

  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
    Read the FAQ on why using feof() to control a loop is bad.

    Use the return result of fgets() instead.
    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
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Better to just check if buf[0] == '\n'.
    This is also C, not C++.

  4. #4
    Registered User
    Join Date
    Jul 2007
    Posts
    15
    Thanks. I got it.

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. Blank lines "\n"
    By Coding in forum C++ Programming
    Replies: 15
    Last Post: 02-18-2008, 08:56 PM
  3. Line Counting
    By 00Sven in forum C Programming
    Replies: 26
    Last Post: 04-02-2006, 08:59 PM
  4. Question About Blank Lines in Text Files
    By Zildjian in forum C++ Programming
    Replies: 11
    Last Post: 10-16-2004, 04:31 PM
  5. Blank Lines in a menu
    By ColdFire in forum Windows Programming
    Replies: 2
    Last Post: 07-05-2002, 03:51 PM