473,395 Members | 1,762 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

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

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
Nov 13 '05 #1
4 10182
On 5 Oct 2003 18:28:33 -0700, ce****@transy.edu (Charles Erwin) wrote:
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
Nov 13 '05 #2
On Sun, 5 Oct 2003, Charles Erwin wrote:
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.

--
Send e-mail to: darrell at cs dot toronto dot edu
Don't send e-mail to vi************@whitehouse.gov
Nov 13 '05 #3
Darrell Grainger wrote:

On Sun, 5 Oct 2003, Charles Erwin wrote:
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.


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.

--
Er*********@sun.com
Nov 13 '05 #4
Darrell Grainger wrote:

On Sun, 5 Oct 2003, Charles Erwin wrote:
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.

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.
--
Joe Wright http://www.jw-wright.com
"Everything should be made as simple as possible, but not simpler."
--- Albert Einstein ---
Nov 13 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Rob Somers | last post by:
Hey all I am writing a program to keep track of expenses and so on - it is not a school project, I am learning C as a hobby - At any rate, I am new to structs and reading and writing to files,...
4
by: Psibur | last post by:
Hello, trying to get back into c and was having issue with reading a simple text file with an aribtrary # of lines with 3 int's per line, with the eventual purpose of putting each int into an...
7
by: Thomas Sourmail | last post by:
Hi, I hope I am missing something simple, but.. here is my problem: I need my program to check the last column of a file, as in : a b c d target ref 0 0 0 0 1 a 1 0 0 0 1.5 b 2 0 0 0 2 c
7
by: Kay | last post by:
1) If i want to read data from a txt file, eg John; 23; a Mary; 16; i How can I read the above data stopping reading b4 each semi-colon and save it in three different variables ? 2) If I...
7
by: bhanuprakash | last post by:
I am trying to use fscanf to read my test file. In my test file i sometimes have blank lines. When I try to read using the following format. fscanf(fp,"%\n",temp_str); If there is any blank...
9
by: uidzer0 | last post by:
Hey everyone, Taken the following code; is there a "proper" or dynamic way to allocate the length of line? #include <stdio.h> #include <errno.h> int main(int argc, char **argv) { FILE *fp;
9
by: kvnsmnsn | last post by:
Over the course of my career I've transitioned from an Ada programmer (am I dating myself?) to a C programmer to a Java programmer and now back to a C programmer with the job I've currently...
59
by: David Mathog | last post by:
Apologies if this is in the FAQ. I looked, but didn't find it. In a particular program the input read from a file is supposed to be: + 100 200 name1 - 101 201 name2 It is parsed by reading...
5
by: a | last post by:
After reading FAQ comp.lang.c section 12 and googling again, still there is no threads talking about reading a series of numbers. The input files, somehow structured, is exemplified below: ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.