Reading empty lines from a file

This is a discussion on Reading empty lines from a file within the C Programming forums, part of the General Programming Boards category; I'm trying to read text from a file using stream variables and fscanf. This file has occasional empty lines, for ...

  1. #1
    Registered User
    Join Date
    Mar 2012
    Posts
    26

    Reading empty lines from a file

    I'm trying to read text from a file using stream variables and fscanf. This file has occasional empty lines, for example:

    Code:
    This is line 1.
    
    This is line 2.
    This is line 3.
    
    This is line 4.
    So far, I'm able to read everything from the file, save for the empty lines. So my output becomes:

    Code:
    This is line 1.
    This is line 2.
    This is line 3.
    This is line 4.
    This is the code I use for the actual reading (I'm using the while(!feof(FILE)) loop; I know it's bug-prone but that's what we were taught):
    Code:
                fscanf(FILE, "%[^\n]\n", Line);
                printf("  %s\n", Line);
    My question is, how do I read the empty lines?

  2. #2
    Registered User
    Join Date
    Mar 2010
    Posts
    535
    My personal preference would be to use fgets() to read the a whole line at a time, then use sscanf (scanf from a string rather than a file) on the buffer to do any regular expression parsing that you need to do. Or at the moment you could just check directly that the first character of the line isn't a newline, and only print the line in that case). Depends if you'll need to use sscanf on the input later - if so then leave the scanf (in form of sscanf)) there by all means.

    Think there may be other opinions from people on how to best do this - that's just my 2 cents! If you need any more help/info let me know.

  3. #3
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    fscanf() returns the number of objects scanned and stored successfully.

    In the case of an empty line, nothing will have been stored, so the return value will be zero. Test that return value from fscanf(), and if it's zero, then print out a newline. If the return is > 0 then print the string followed by a newline (as you currently do).

    If the return is EOF, then exit the reading loop, you've reached the end of the file.

  4. #4
    Registered User
    Join Date
    Mar 2010
    Posts
    535
    Yep, that'd work too. The reason I shied away from it is because when fscanf doesn't read anything (returns 0) it leaves stdin unmodified, effectively just leaving the newline from the empty line hanging there. You'll need to deal with that -- I guess just an extra getc() will do it. I hate trying to figure out how much input I've gobbled up and what's still lurking, hence the fgets suggestion. I know how it should work, but I always seem to forget some nuance and end up with a newline spanner in the works.

  5. #5
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Oh it definitely can happen, SmokyAngel. Those little details can always trip us up if we're not careful.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. deleting empty lines from a file
    By smoking81 in forum C Programming
    Replies: 4
    Last Post: 03-09-2008, 01:10 AM
  2. Keep reading lines in a file
    By bigmac(rexdale) in forum C Programming
    Replies: 18
    Last Post: 12-01-2007, 01:05 PM
  3. Reading Lines from a file
    By phoenix-47 in forum C++ Programming
    Replies: 3
    Last Post: 12-13-2005, 02:32 PM
  4. Reading Lines From a File
    By Dangerous Dave in forum C++ Programming
    Replies: 6
    Last Post: 02-22-2005, 12:17 PM
  5. Reading Certain Lines In File?
    By SyntaxBubble in forum Windows Programming
    Replies: 8
    Last Post: 02-06-2002, 10:02 PM

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