Discarding blank lines?

This is a discussion on Discarding blank lines? within the C++ Programming forums, part of the General Programming Boards category; Hey, I'm writing a program that takes input from a file called index.txt. I want to be able to discard ...

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

  3. #3
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,768
    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, 10:51 AM
  2. Blank lines "\n"
    By Coding in forum C++ Programming
    Replies: 15
    Last Post: 02-18-2008, 07: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

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