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!