C programming - reading from a file - help request!

J

James Tunic

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");

}

}
 
M

Mark McIntyre

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.
 
E

Ekkehard Morgenstern

James Tunic said:
&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. :)
 
E

Ekkehard Morgenstern

Ekkehard Morgenstern said:
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 "= ="
 

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

No members online now.

Forum statistics

Threads
473,954
Messages
2,570,116
Members
46,704
Latest member
BernadineF

Latest Threads

Top