file reading problem

This is a discussion on file reading problem within the C Programming forums, part of the General Programming Boards category; hey, total c beginner so please bare with me, im having trouble creating a program to read text files and ...

  1. #1
    Registered User
    Join Date
    Nov 2005
    Posts
    5

    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

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    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.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Nov 2005
    Posts
    5
    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

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    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.
    Hope is the first step on the road to disappointment.

  5. #5
    Registered User
    Join Date
    Nov 2005
    Posts
    5
    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

  6. #6
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    You know that
    Code:
    printf("Hello, World!\n");
    is the same as
    Code:
    fprintf(stdout, "Hello, World!\n");
    right?
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. File transfer- the file sometimes not full transferred
    By shu_fei86 in forum C# Programming
    Replies: 13
    Last Post: 03-13-2009, 12:44 PM
  2. Post...
    By maxorator in forum C++ Programming
    Replies: 12
    Last Post: 10-11-2005, 08:39 AM
  3. Replies: 20
    Last Post: 06-12-2005, 11:53 PM
  4. Replies: 3
    Last Post: 03-04-2005, 01:46 PM
  5. Problem reading file
    By winsonlee in forum C Programming
    Replies: 2
    Last Post: 04-23-2004, 06:52 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21