Thread: Discarding blank lines?

  1. #1
    Registered User
    Join Date
    Jul 2007

    Discarding blank lines?


    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:

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

    Ex, it catches


    but doesn't catch


    Any help would be appreciated. Thanks.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    The edge of the known universe
    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
    Join Date
    Oct 2007
    Inside my computer
    Better to just check if buf[0] == '\n'.
    This is also C, not C++.

  4. #4
    Registered User
    Join Date
    Jul 2007
    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