Mombu the Programming Forum sponsored links

Go Back   Mombu the Programming Forum > Programming > C programming - reading from a file - help request!
User Name
Password
REGISTER NOW! Mark Forums Read

sponsored links


Reply
 
1 21st May 05:38
benjamin robson
External User
 
Posts: 1
Default C programming - reading from a file - help request!


[this first paragraph was in a separate post. -mod]
Sorry I am using a friends computer, can anyone who replies either reply to
jamestunic@hotmail.com or the newsgroup.

I am trying to write a program (in C) for reading grid1.dat and grid2.dat
and displaying 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

Ben.

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

#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");

}

}
--
comp.lang.c.moderated - moderation address: clcm@plethora.net
  Reply With Quote


  sponsored links


2 1st June 00:23
francis glassborow
External User
 
Posts: 1
Default C programming - reading from a file - help request!


In article <clcm-20031124-0013@plethora.net>, Benjamin Robson
<bungalow.legends@ntlworld.com> writes

A little more use of indentation and less use of newlines would help.

always, always initialise pointers.

but what happens if it isn't, i.e. the file is not opened for reading?
Try rewriting as:

if((input_stream = fopen(filename, "r")) == NULL){
puts("Could not open input file");
return EXIT_FAILURE;
}

and remove the following brace as the rest now uses a file that could be
opened. I.e. eliminate the bad case rather than check you have the good
one.


but this already eliminates cases where line_ptr is NULL so there is no
point in latter checks for it. Oh I see, you are doing all your
processing inside the while. Don't do that, learn to write simple short
statements it makes you code much easier to follow.

{


return EXIT_FAILURE;
}


{

return EXIT_FAILURE;
}

return EXIT_SUCCESS;


--
Francis Glassborow ACCU
If you are not using up-to-date virus protection you should not be reading
this. Viruses do not just hurt the infected but the whole community.
--
comp.lang.c.moderated - moderation address: clcm@plethora.net
  Reply With Quote
3 1st June 00:24
francis glassborow
External User
 
Posts: 1
Default C programming - reading from a file - help request!


In article <clcm-20031124-0013@plethora.net>, Benjamin Robson
<bungalow.legends@ntlworld.com> writes

A little more use of indentation and less use of newlines would help.

always, always initialise pointers.

but what happens if it isn't, i.e. the file is not opened for reading?
Try rewriting as:

if((input_stream = fopen(filename, "r")) == NULL){
puts("Could not open input file");
return EXIT_FAILURE;
}

and remove the following brace as the rest now uses a file that could be
opened. I.e. eliminate the bad case rather than check you have the good
one.


but this already eliminates cases where line_ptr is NULL so there is no
point in latter checks for it. Oh I see, you are doing all your
processing inside the while. Don't do that, learn to write simple short
statements it makes you code much easier to follow.

{


return EXIT_FAILURE;
}


{

return EXIT_FAILURE;
}

return EXIT_SUCCESS;


--
Francis Glassborow ACCU
If you are not using up-to-date virus protection you should not be reading
this. Viruses do not just hurt the infected but the whole community.
--
comp.lang.c.moderated - moderation address: clcm@plethora.net
  Reply With Quote
Reply


Thread Tools
Display Modes




Copyright 2006 SmartyDevil.com - Dies Mies Jeschet Boenedoesef Douvema Enitemaus -
666