-
file reading problem
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
-
It appears you're trying to do more than simply display a file. So what are you really trying to do?
Code:
#include<stdio.h>
int main( int argc, char **argv )
{
if( argc != 2 )
{
printf("Usage: %s <file to display>\n", argv[0] ? argv[0] : "programname" );
}
else
{
FILE *fp = fopen( argv[1], "r" );
if( fp == NULL )
{
printf("Unable to open file \'%s\'\n", argv[1] );
}
else
{
int c;
while( (c = fgetc( fp )) != EOF )
putchar( c );
fclose( fp );
}
}
return 0;
}
Quzah.
-
basically to 'carefully read' the file
the program asks the user to select a file and then opens that file. it then performs a test using a loop to check that there are 3 numbers on each line until it reaches the end of the file at which point it prints the numbers. if there are not 3 numbers on a line it jumps out of the loop and displays an error and if the line is blank it shows the end of file msg
-
Well, you don't need a struct, unless you're planning on expaning this somehow. Generally, I'd work from the simplest implementation, and then add to it. Also, the use of global variables is usually frowned upon. It's better to pass variables as arguments if you need them in another function.
Code:
char buf[BUFSIZ] = {0};
while( fgets( buf, BUFSIZ, yourfile ) != NULL )
{
int v1, v2, v3;
if ( sscanf( buf, "%d %d %d", &v1, &v2, &v3 ) != 3 )
{
printf( "Invalid data in file!\n" );
break;
}
else
{
printf("v1 is %d, v2 is %d, v3 is %d\n", v1, v2, v3 );
}
}
You can modify it to use something other than integers if you like. This was just a quick and dirty example.
Quzah.
-
id love to do that but unfortunately life aint that simple. one of the specifications is that the program must contain the struct, ill give you the break down of what im trying to do at each point and see if thats any help
Code:
struct node node_array[100]; //declare array using 'node' datatype//
int no_nodes = 0, no_values, i;
FILE *input_stream;
fprintf(stdout, "Enter file name:"); //read file
fscanf(stdin, "%s", filename); from user//
fprintf(stdout, "id x y\n"); //delete this line it isnt supposed to be there anymore//
if ((input_stream = fopen(filename, "r")) != NULL)
{
fgets(line, sizeof(line), input_stream); //read table heading, then ignore it//
while (((line_ptr = fgets(line, sizeof(line), input_stream)) != NULL) &&
((no_values = sscanf (line, "%d %lf %lf", //read from the current line
&node_array[no_nodes].id, store in the current array
&node_array[no_nodes].x, element and increment the
&node_array[no_nodes].y)) == 3)) no_nodes++; array index//
if ((line_ptr != NULL) && (no_values != 3)) //if the number of values on a line
fprintf(stdout, "Error reading line %s\n", line); doesnt =3, display error//
else
if (line_ptr == NULL) //if the line is blank
fprintf(stdout, "End of file found\n"); display end of file//
}
for( i = 0; i < no_nodes; i++); //is a print loop
{ which im pretty
fprintf(stdout, "%d %lf %lf\n", node_array[i].id ,node_array[i].x ,node_array[i].y ); sure is wrong//
}
the line
fprintf(stdout, "id x y\n");
is obsolete, i was going to use this method to display titles before
i found out how to ignore them in the test
they are now part of the text file
the line:
fgets(line, sizeof(line), input_stream);
is because at the top of the text file the colomns have headers,
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
Hope that helps and if not, thanks anyway Quzah
-
You know that
Code:
printf("Hello, World!\n");
is the same as
Code:
fprintf(stdout, "Hello, World!\n");
right?