fscanf or fgets still misses last line unless there is a newline

C

Charles Erwin

Is there any way, upon scanning in a file line by line to avoid
missing the last line if there is not a newline character (aka you
have to hit return on the last line of input in your file). I was
sure that there was a way around it but it escapes me if there is one.

Thanks
Charles Erwin
 
R

Robert W Hand

Is there any way, upon scanning in a file line by line to avoid
missing the last line if there is not a newline character (aka you
have to hit return on the last line of input in your file). I was
sure that there was a way around it but it escapes me if there is one.

I believe that it is one of those implementation-defined issues. A
text stream is composed of lines that end with a new-line character.
Whether the last line requires a new-line character is
implementation-defined. The standard requires the data read from a
text file to be identical to the data read into the file only if the
last character is a new-line character plus other restrictions on the
type of characters and on sequencing of spaces and new-lines
previously written into the file. So I do not think that the standard
can help you here.

You might try reading character by character or checking with your
implementation documents.

Best wishes,

Bob
 
D

Darrell Grainger

Is there any way, upon scanning in a file line by line to avoid
missing the last line if there is not a newline character (aka you
have to hit return on the last line of input in your file). I was
sure that there was a way around it but it escapes me if there is one.

When you are dealing with text streams it is implementation-defined as to
whether or not the last line requires a terminating new-line character
(see 7.19.2 streams, paragraph 2).

The tough part is that EOF or a read error will return a NULL from fgets.
You cannot tell which happened. You could write your code such that it
uses fgets but after a failed fgets you use fgetc to see if it is really
end of file or a line without a newline character. If the latter, continue
to use fgetc to read in the remaining data.
 
E

Eric Sosman

Darrell said:
When you are dealing with text streams it is implementation-defined as to
whether or not the last line requires a terminating new-line character
(see 7.19.2 streams, paragraph 2).

The tough part is that EOF or a read error will return a NULL from fgets.
You cannot tell which happened.

Sur you can: use feof() and/or ferror().
You could write your code such that it
uses fgets but after a failed fgets you use fgetc to see if it is really
end of file or a line without a newline character. If the latter, continue
to use fgetc to read in the remaining data.

The requirement that every line of text end with a newline
(if the implementation has such a requirement) applies to the
stream, not to the method used to read it. That is, if fgets()
is unable to read the unterminated line, [f]getc() may also be
unable to read it.
 
J

Joe Wright

Darrell said:
When you are dealing with text streams it is implementation-defined as to
whether or not the last line requires a terminating new-line character
(see 7.19.2 streams, paragraph 2).

The tough part is that EOF or a read error will return a NULL from fgets.
You cannot tell which happened. You could write your code such that it
uses fgets but after a failed fgets you use fgetc to see if it is really
end of file or a line without a newline character. If the latter, continue
to use fgetc to read in the remaining data.
No. In general, fgets() returns NULL after successfully reading the
file.

The only line read by fgets() not terminated by '\n' is the last one.
There is no other possibility. In this case we have read EOF or we have
a read error but fgets() returns its buffer (terminated with '\0'), not
NULL. If you want to know whether fgets() got a '\n' you have to
strchr() for it. If you don't find '\n' you can assume that fgets() is
over. The question is 'last line without newline?' or 'read error?'.
ferror() and feof() provide those answers.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,051
Latest member
CarleyMcCr

Latest Threads

Top