Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Missing Lines With fscanf()

Reply
Thread Tools

Missing Lines With fscanf()

 
 
Psibur
Guest
Posts: n/a
 
      06-04-2004
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 element of an
array (eventually will be other things, but I'm sticking to int's for
now). I.e.:
0 1 1
1 1 1
2 1 1 etc...

The problem is it'll read and print all but the last line. Is there
something I'm forgetting about an End Of Line or EOF with fscanf? If
it means anything, I'm using gcc on win32 (djgpp) and haven't been
able to compile it under Linux yet (if there's much difference.) Here
is the minimalist version of the code with all the fault protection
and good stuff taken out for brevity:

#include <stdio.h>

int main() {
char buff[BUFSIZ];
FILE *infile;
int nums[10];

infile = fopen("a.dat", "r");
fscanf(infile, "%d %d %d\n", &nums[0],&nums[1], &nums[2]);
while (!feof(infile)) {
/*future home of parsing junk*/
printf("%d %d %d\n", nums[0], nums[1], nums[2]);
fscanf(infile, "%d %d %d\n", &nums[0], &nums[1], &nums[2]);
}
fclose(infile);

return 0;
}

Much appreciation in advance.
 
Reply With Quote
 
 
 
 
Eric Sosman
Guest
Posts: n/a
 
      06-04-2004
Psibur wrote:
> 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 element of an
> array [...]
> The problem is it'll read and print all but the last line. Is there
> something I'm forgetting about an End Of Line or EOF with fscanf? If
> it means anything, I'm using gcc on win32 (djgpp) and haven't been
> able to compile it under Linux yet (if there's much difference.) Here
> is the minimalist version of the code with all the fault protection
> and good stuff taken out for brevity:
>
> #include <stdio.h>
>
> int main() {
> char buff[BUFSIZ];
> FILE *infile;
> int nums[10];
>
> infile = fopen("a.dat", "r");
> fscanf(infile, "%d %d %d\n", &nums[0],&nums[1], &nums[2]);
> while (!feof(infile)) {
> /*future home of parsing junk*/
> printf("%d %d %d\n", nums[0], nums[1], nums[2]);
> fscanf(infile, "%d %d %d\n", &nums[0], &nums[1], &nums[2]);
> }
> fclose(infile);
>
> return 0;
> }


The two problems I can see are addressed in Questions 12.17
and 12.2 of the comp.lang.c Frequently Asked Questions (FAQ) list:

http://www.eskimo.com/~scs/C-faq/top.html

(To understand what role 12.2 plays here, I think you'd do better
to study 12.17 first.)

--
http://www.velocityreviews.com/forums/(E-Mail Removed)

 
Reply With Quote
 
 
 
 
CBFalconer
Guest
Posts: n/a
 
      06-04-2004
Psibur wrote:
>
> 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 element of an
> array (eventually will be other things, but I'm sticking to int's for
> now). I.e.:
> 0 1 1
> 1 1 1
> 2 1 1 etc...
>
> The problem is it'll read and print all but the last line. Is there
> something I'm forgetting about an End Of Line or EOF with fscanf? If
> it means anything, I'm using gcc on win32 (djgpp) and haven't been
> able to compile it under Linux yet (if there's much difference.) Here
> is the minimalist version of the code with all the fault protection
> and good stuff taken out for brevity:
>
> #include <stdio.h>
>
> int main() {
> char buff[BUFSIZ];
> FILE *infile;
> int nums[10];
>
> infile = fopen("a.dat", "r");
> fscanf(infile, "%d %d %d\n", &nums[0],&nums[1], &nums[2]);
> while (!feof(infile)) {
> /*future home of parsing junk*/
> printf("%d %d %d\n", nums[0], nums[1], nums[2]);
> fscanf(infile, "%d %d %d\n", &nums[0], &nums[1], &nums[2]);
> }
> fclose(infile);
>
> return 0;
> }


Try (untested):

#include <stdio.h>

int main(void)
{
FILE *infile;
int nums[10];

if (infile = fopen("a.dat", "r")) {
while (3 == fscanf(infile, "%d %d %d\n",
&nums[0], &nums[1], &nums[2])) {
/*future home of parsing junk*/
printf("%d %d %d\n", nums[0], nums[1], nums[2]);
}
fclose(infile);
}
return 0;
}

NEVER fail to test the result of an input operation. USUALLY test
the result of any system call.

--
"The most amazing achievement of the computer software industry
is its continuing cancellation of the steady and staggering
gains made by the computer hardware industry..." - Petroski


 
Reply With Quote
 
Darrell Grainger
Guest
Posts: n/a
 
      06-07-2004
On Fri, 4 Jun 2004, Psibur wrote:

> 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 element of an
> array (eventually will be other things, but I'm sticking to int's for
> now). I.e.:
> 0 1 1
> 1 1 1
> 2 1 1 etc...
>
> The problem is it'll read and print all but the last line. Is there
> something I'm forgetting about an End Of Line or EOF with fscanf? If
> it means anything, I'm using gcc on win32 (djgpp) and haven't been
> able to compile it under Linux yet (if there's much difference.) Here
> is the minimalist version of the code with all the fault protection
> and good stuff taken out for brevity:
>
> #include <stdio.h>
>
> int main() {
> char buff[BUFSIZ];
> FILE *infile;
> int nums[10];
>
> infile = fopen("a.dat", "r");
> fscanf(infile, "%d %d %d\n", &nums[0],&nums[1], &nums[2]);
> while (!feof(infile)) {
> /*future home of parsing junk*/
> printf("%d %d %d\n", nums[0], nums[1], nums[2]);
> fscanf(infile, "%d %d %d\n", &nums[0], &nums[1], &nums[2]);
> }
> fclose(infile);
>
> return 0;
> }


The fscanf in the while loop triggers EOF on reading the last line. After
the last fscanf it goes to the top of the loop, the exit condition is met
and you leave the loop before printing the last lines.

I usually use fgets to read a line then parse it. Another option is to get
the result from fscanf and see how many directives were successful, e.g.

result = fscanf(infile, "%d %d %d\n", &num[0], &num[1], &num[2]);

then exit if result != 3. I'd actually use the fscanf in the while
expression rather than having two calls, e.g.

while(fscanf(infile, "%d %d %d\n", &num[0], &num[1], &num[2]) == 3) {
printf("%d %d %d\n", nums[0], nums[1], nums[2]);
}

This will work even if the last line does not contain a newline character.
It will not print out a line that only has 1 or 2 numbers on it. Something
like:

1 0 1
1 2 1
1 3
1 2 0

will quit after the second line is read.

--
Send e-mail to: darrell at cs dot toronto dot edu
Don't send e-mail to (E-Mail Removed)
 
Reply With Quote
 
Chris Torek
Guest
Posts: n/a
 
      06-11-2004
In article <news(E-Mail Removed)>
Darrell Grainger <(E-Mail Removed)> writes:
>I usually use fgets to read a line then parse it.


(I think this is usually the best approach.)

>Another option is to get the result from fscanf and see how many
>directives were successful ...


This has some pitfalls, including one you ran into here:

>rather than having two calls, e.g.
> while(fscanf(infile, "%d %d %d\n", &num[0], &num[1], &num[2]) == 3) {
> printf("%d %d %d\n", nums[0], nums[1], nums[2]);
> }
>This will work even if the last line does not contain a newline character.


True (assuming the implementation supports such lines in the first
place), but this:

>It will not print out a line that only has 1 or 2 numbers on it. Something
>like:
>
>1 0 1
>1 2 1
>1 3
>1 2 0
>
>will quit after the second line is read.


is not the case.

The problem lies in the scanf engine's interpretation of "white
space", which disagrees with what most programmers believe is the
"most reasonable" interpretation.

The directive sequence "%d %d %d\n" does *not* mean "three
integers separated by blanks and terminated by a newline." The
final "\n", for instance, means "as much blank space as possible,
including any arbitrary number of newlines, and including no newlines."

The blanks separating the "%d"s *also* mean "as much blank space
as possible, including any arbitrary number of newlines, and including
no newlines."

That means that the third input line -- "1 3\n" -- will be treated
as the number 1 (first "%d"), one blank (first " "), the number 3
(second "%d"), one newline (second " ")... and then the fourth input
line's "1" will be read and converted to satisfy the third "%d". The
newline in the scanf directive will then match the blank after the
1, leaving "2 0\n" in the input stream.

One might attempt to fix this by rewriting the call as:

while (fscanf(infile, "%d%d%d\n", ...) ...)

so that there is no " " directive to match the newline after the
two integers on the third input line ("1 3\n"). But this is no
help, because %d directives include an implied whitespace directive
first. "%d%d%d\n" means exactly the same thing to scanf as
"%d %d %d\n", or even " %d%d \v %d \t\f\r\n"! *All* whitespace
in format directives is equivalent, and any whitespace is equivalent
to an arbitrarily long string of whitespace. Most scanf directives
-- the exceptions are %c and %[ -- have an "implied blank" in front
of them. A final "\n" in a scanf directive means the same thing as
a trailing blank, or "\t", or "\f".

What this all boils down to is that *only* the %c and %[ directives
give you control over "lines". *All* other scanf conversions will
behave poorly in any kind of file-parsing or interactive application.
In other words, these are the only *useful* directives -- unless
you first read in a line, then use sscanf() to pick it apart. (By
using sscanf(), you avoid the problem, because you have a tightly
controlled string in memory, rather than an uncontrolled input
stream coming from a file or user-at-keyboard or whatnot.)
--
In-Real-Life: Chris Torek, Wind River Systems
Salt Lake City, UT, USA (4039.22'N, 11150.29'W) +1 801 277 2603
email: forget about it http://web.torek.net/torek/index.html
Reading email is like searching for food in the garbage, thanks to spammers.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to know two lines are a pare parallel lines lovecreatesbeauty C Programming 11 04-28-2006 03:32 AM
How to display a string in many lines, each lines have a specified length thuyptt@dsp.com.vn C++ 1 12-06-2005 07:26 AM
Asp.Net Calender, how to display 5 lines if there are only 5 lines in one month? Jack ASP .Net 9 10-12-2005 03:44 AM
Modems, Analog Lines and ... Electrical Lines? Sens Fan Happy In Ohio Computer Support 5 09-02-2004 04:15 AM
Re: how to read 10 lines from a 200 lines file and write to a new file?? Joe Wright C Programming 0 07-27-2003 08:50 PM



Advertisments