Hi , new to the forums:)
Trying to do this tutorial from the 'C programming for scientists and engineers' book. The person who wrote it is my lecturer.
The program reads the file, but when it displays it, it only shows the last line - I am stuck!
I think the problem is to do with the last for loop.

#include <stdio.h>
#include <stdlib.h> 

int main(void)
{
char line[101], filename[101];
char *line_ptr;


struct node
{
int id;
double x, y;
};
double dummy = 9.9;
int i=0;

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

fprintf(stdout, "--------------------------------\n");
fprintf(stdout, "Here is the data from %s:\n", filename);
for (i=0; i<no_nodes; i++)
fprintf(stdout, "%d %lf %lf\n",node_array[i].id,node_array[i].x,node_array[i].y);



return(0);
}

The dat file contains
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


Thanks!

Recommended Answers

All 3 Replies

Congratulations on using:
- Code tags for your first post.
- int main(void) instead of the usual void main() for your first post.
You have a bright future ahead! :-)

Anyways, your problem is a rogue semi-colon sitting at the end of your while construct. Remove it and surround your increment statement with braces, it should turn out to be fine. Or if you want to do it all in a single while, just add a && ++no_nodes at the end.

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

The code could have been written in a better/efficient way but I'll leave it to the regulars to the C forum to sort that out for you.

The tutorial is to implement a semi-complete program from the lecture notes, which closely follow the text book.
No idea how to display each member of the array structure on the screen though!

I always use int main (void):)

Congratulations on using:
- Code tags for your first post.
- int main(void) instead of the usual void main() for your first post.
You have a bright future ahead! :-)

Agreed! But loose a point for bad formatting, making your code very hard to follow.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.