473,320 Members | 1,846 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

C programming - reading from a file - help request!

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.
(N.B. I'm limited to using only the functions contained within the current
program)
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





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

}

}
Nov 13 '05 #1
3 6066
On Tue, 25 Nov 2003 00:12:42 -0000, in comp.lang.c , "James Tunic"
<ja********@hotmail.com> wrote:
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++;


This while is much too complicated, too much is going on in the
conditional part, and some of it is incorrect. Break it down into a
simpler while with a larger body, and part of your problem will be
revealed. You will need to set line_ptr and no_values to sensible
values first by the way.
Also, sscanf does not handle whitespace quite how you think it does.
--
Mark McIntyre
CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>
CLC readme: <http://www.angelfire.com/ms3/bchambless0/welcome_to_clc.html>
Nov 13 '05 #2

"James Tunic" <ja********@hotmail.com> schrieb im Newsbeitrag
news:28********************@newsfep1-win.server.ntli.net...
&node_array [no_nodes].y)) = =3)) no_nodes++;


The body of your while loop consists of only one statement "no_nodes++". The
statements after "no_nodes++" are executed after the loop has finished.
That's why it's best always to bracket the loop body using { }.

Another problem might be that you have a blank between the two "= =". That's
wrong, it should read "==" instead. Otherwise the compile might generate two
assignments instead of the comparison. The compiler is not strictly ANSI
conformant, because it should report "= =" as a syntax error.

So, your loop should look like this:

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 ) ) { /* <-- open { */
no_nodes++;
if ( (line_ptr !=NULL) && ( no_values !=3 ) )
{ /* <-- bracket here too for clarity */
fprintf(stdout, "Error reading line
%s\n", line);
}
else {
if (line_ptr= =NULL) fprintf(stdout,
"End of file found\n");
}
} /* <-- closing while() */

This way it should work. :-)
Nov 13 '05 #3

"Ekkehard Morgenstern" <ek******************@onlinehome.de> schrieb im
Newsbeitrag news:bq**********@online.de...
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)) ==
The "==" should be used instead of "= =".
3 ) ) { /* <-- open { */
no_nodes++;
if ( (line_ptr !=NULL) && ( no_values !=3 ) )
{ /* <-- bracket here too for clarity */
fprintf(stdout, "Error reading line
%s\n", line);
}
else {
if (line_ptr==NULL) fprintf(stdout,
here, as well. :-)

"==" instead of "= ="
"End of file found\n");
}
} /* <-- closing while() */

This way it should work. :-)

Nov 13 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Joel Goldstick | last post by:
I wanted to write a simple page to let me choose a directory and then list the files in it. The end goal was to make an easy way to copy all the file names in a directory. I tested with Opera7,...
6
by: John Walton | last post by:
Hello, everyone. I just began school, and they already assigned us science fair. Since I'm in 8th grade, I get to do demonstrations for our projects. I'm probably going to demonstrate Python's...
3
by: Drewdog | last post by:
I am getting some error messages which I can't figure out their meaning. I have the code setup, I think it's correct but it doesn't work. My goal is to get this program to read from a data file and...
0
by: Neo | last post by:
Hello: I am receiving a Binary File in a Request from a application. The stream which comes to me has the boundary (Something like "---------------------------390C0F3E0099" without the quotes),...
3
dmjpro
by: dmjpro | last post by:
plz send me a good link which can clearify me how the J2EE framework works i want the details information .... plz help thanx
17
by: CoreyWhite | last post by:
I bought this book years ago, when I was just learning C++. Since then I've gone through every math course offered at my college, taken courses on coding C & thinking in terms how how to make the...
8
by: =?Utf-8?B?Sm9obg==?= | last post by:
Hi all, I am new to .net technologies. ASP.NET supports socket programming like send/receive in c or c++? I am developing web-site application in asp.net and code behind is Visual C#. In...
270
by: jacob navia | last post by:
In my "Happy Christmas" message, I proposed a function to read a file into a RAM buffer and return that buffer or NULL if the file doesn't exist or some other error is found. It is interesting...
1
by: shyaminf | last post by:
hi everybody! iam facing a problem with the transfer of file using servlet programming. i have a code for uploading a file. but i'm unable to execute it using tomcat5.5 server. kindly help me how to...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.