hey, total c beginner so please bare with me, im having trouble creating a program to read text files and display them on screen
Code:
#include <stdio.h>
char line[101], filename[101];
char *line_ptr;
struct node
{
int id;
double x, y;
};
int main(void)
{
struct node node_array[100];
int no_nodes = 0, no_values, i;
FILE *input_stream;
fprintf(stdout, "Enter file name:");
fscanf(stdin, "%s", filename);
fprintf(stdout, "id x y\n");
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");
}
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 );
}
fclose(input_stream);
return(0);
}
there are 2 text files that it needs to read
grid1.dat is a 3x9 table of values
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
grid2.dat is similar with 12 rows instead of 9
if anyone can see anything glaringly obvious please let me know
thanks
mick