By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,797 Members | 1,794 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,797 IT Pros & Developers. It's quick & easy.

C programming - reading from a file - help request!

P: n/a
I am trying to write a program (in C) for reading grid1.dat and grid2.dat
and displaying the data it has read on the screen.

I have got as far as the program listed below, it seems to compile fine but
doesn't seem to read correctly as most of the time a runtime error occurs. I
'm also unsure how to implement the fprintf statement to get the data to be
displayed on the screen.

Any help or advice would be greatly appreciated.
(N.B. I'm limited to using only the functions contained within the current
program)
Many thanks
James

Info stored in grid1.dat:

id x y

1 1.2 0.8

2 1.2 1.0

3 1.2 1.2

4 1.4 0.8

5 1.4 1.0

6 1.4 1.2

7 1.6 0.8

8 1.6 1.0

9 1.6 1.2

Info stored in grid2.dat:

id x y

1 1.2 0.8

2 1.2 1.0

3 1.2 1.2

4 1.4 0.8

5 1.4 1.0

6 1.4 1.2

7 1.6 0.8

8 1.6 1.0

9 1.6 1.2

10 1.8 0.8

11 1.8 1.0

12 1.8 1.2





#include <stdio.h>

#include <stdlib.h>

int main(void)

{

char line[101], filename[101];

char*line_ptr;

struct node

{

int id;

double x, y;

};

struct node node_array[100];

int no_nodes = 0, no_values;

FILE*input_stream;

fprintf(stdout, "Enter file name:");

fscanf(stdin, "%s", filename);

if ((input_stream = fopen(filename, "r")) !=NULL)

{

fgets(line, sizeof(line), input_stream);

while(((line_ptr = fgets(line, sizeof(line), input_stream))
!=NULL)&&

((no_values = sscanf(line, "%d %lf %lf",
&node_array [no_nodes].id,
&node_array [no_nodes].x,
&node_array [no_nodes].y)) = =3)) no_nodes++;

if ((line_ptr !=NULL)&&(no_values !=3))

fprintf(stdout, "Error reading line %s\n", line);

else

if(line_ptr= =NULL)

fprintf(stdout, "End of file found\n");

}

}
Nov 13 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
On Tue, 25 Nov 2003 00:12:42 -0000, in comp.lang.c , "James Tunic"
<ja********@hotmail.com> wrote:
while(((line_ptr = fgets(line, sizeof(line), input_stream)) !=NULL)&&
((no_values = sscanf(line, "%d %lf %lf",
&node_array [no_nodes].id,
&node_array [no_nodes].x,
&node_array [no_nodes].y)) = =3)) no_nodes++;


This while is much too complicated, too much is going on in the
conditional part, and some of it is incorrect. Break it down into a
simpler while with a larger body, and part of your problem will be
revealed. You will need to set line_ptr and no_values to sensible
values first by the way.
Also, sscanf does not handle whitespace quite how you think it does.
--
Mark McIntyre
CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>
CLC readme: <http://www.angelfire.com/ms3/bchambless0/welcome_to_clc.html>
Nov 13 '05 #2

P: n/a

"James Tunic" <ja********@hotmail.com> schrieb im Newsbeitrag
news:28********************@newsfep1-win.server.ntli.net...
&node_array [no_nodes].y)) = =3)) no_nodes++;


The body of your while loop consists of only one statement "no_nodes++". The
statements after "no_nodes++" are executed after the loop has finished.
That's why it's best always to bracket the loop body using { }.

Another problem might be that you have a blank between the two "= =". That's
wrong, it should read "==" instead. Otherwise the compile might generate two
assignments instead of the comparison. The compiler is not strictly ANSI
conformant, because it should report "= =" as a syntax error.

So, your loop should look like this:

while ( ( ( line_ptr = fgets( line, sizeof(line),
input_stream ) ) !=NULL ) &&
( ( no_values = sscanf( line, "%d %lf %lf", &node_array
[no_nodes].id,
&node_array [no_nodes].x, &node_array [no_nodes].y)) = =
3 ) ) { /* <-- open { */
no_nodes++;
if ( (line_ptr !=NULL) && ( no_values !=3 ) )
{ /* <-- bracket here too for clarity */
fprintf(stdout, "Error reading line
%s\n", line);
}
else {
if (line_ptr= =NULL) fprintf(stdout,
"End of file found\n");
}
} /* <-- closing while() */

This way it should work. :-)
Nov 13 '05 #3

P: n/a

"Ekkehard Morgenstern" <ek******************@onlinehome.de> schrieb im
Newsbeitrag news:bq**********@online.de...
while ( ( ( line_ptr = fgets( line, sizeof(line),
input_stream ) ) !=NULL ) &&
( ( no_values = sscanf( line, "%d %lf %lf", &node_array [no_nodes].id,
&node_array [no_nodes].x, &node_array [no_nodes].y)) ==
The "==" should be used instead of "= =".
3 ) ) { /* <-- open { */
no_nodes++;
if ( (line_ptr !=NULL) && ( no_values !=3 ) )
{ /* <-- bracket here too for clarity */
fprintf(stdout, "Error reading line
%s\n", line);
}
else {
if (line_ptr==NULL) fprintf(stdout,
here, as well. :-)

"==" instead of "= ="
"End of file found\n");
}
} /* <-- closing while() */

This way it should work. :-)

Nov 13 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.