Thread: Help on reading from a file.

  1. #1
    Registered User
    Join Date
    Nov 2003
    Posts
    3

    Help on reading from a file.

    I am trying to write a program (in C) for reading grid1.dat and grid2.dat and displays the data it has read on the screen.

    I have got as far as the program listed below, it seems to compile fine but doesn’t seem to read correctly as most of the time a runtime error occurs. I’m also unsure how to implement the fprintf statement to get the data to be displayed on the screen.

    Any help or advice would be greatly appreciated.

    Many thanks

    James.


    Info stored in grid1.dat:

    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


    Info stored in grid2.dat:

    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
    10 1.8 0.8
    11 1.8 1.0
    12 1.8 1.2






    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main(void)
    {
    char line[101], filename[101];
    char*line_ptr;
    struct node
    	{
    	int id;
    	double x, y;
    	};
    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");
    }
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > as most of the time a runtime error occurs
    And saying what that was would be a big help...

    Well your while loop is overly complex, you're doing too much in the conditional part and leaving the statement almost empty

    Additionally, you're not range checking your node_array array indices.

    Code:
    while ( 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);
        if ( no_values == 3 && no_nodes < 100 ) {
            no_nodes++;
        } else {
            break;
        }
    }
    if ( feof(input_stream) ) {
        // normal EOF
    } else
    if ( ferror(input_stream) ) {
        // read error
    } else
    if ( no_values != 3 ) {
        // conversion error
    } else
    if ( no_nodes == 100 ) {
        // array full
    }
    > I’m also unsure how to implement the fprintf statement to get the data to be displayed on the screen.
    Hard to believe you got this far with sscanf without knowing at least how to try it.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Nov 2003
    Posts
    9
    hi,

    i think i have solved ur problem. ur problem if i have understood is how to use fprintf in ur code. it is very simple buddy just some kind of a logic is required and u can do it......

    ur whole code goes like this..............

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main(void)
    {
    	char line[101], filename[101];
    	char*line_ptr;
    	struct node
    	{
    		int id;
    			double x, y;
    	};
    	
    	struct node node_array[100];
    	int no_nodes = 0, no_values=0;
    	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)
            {
              if((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");
              else
                  fprintf(stdout,"%d %lf %lf\n",node_array [no_nodes-1].id,node_array [no_nodes-1].x,node_array [no_nodes-1].y);
            }
        }
        system("pause");
    }
    i think this will work for u and hope i i have solved ur problem sucessfully

    bye
    Praveen

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > i think this will work for u and hope i i have solved ur problem sucessfully
    It's "you" and "your"
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. opening empty file causes access violation
    By trevordunstan in forum C Programming
    Replies: 10
    Last Post: 10-21-2008, 11:19 PM
  2. Formatting the contents of a text file
    By dagorsul in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2008, 12:36 PM
  3. Replies: 3
    Last Post: 03-04-2005, 02:46 PM
  4. Possible circular definition with singleton objects
    By techrolla in forum C++ Programming
    Replies: 3
    Last Post: 12-26-2004, 10:46 AM
  5. System
    By drdroid in forum C++ Programming
    Replies: 3
    Last Post: 06-28-2002, 10:12 PM