By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,131 Members | 1,830 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,131 IT Pros & Developers. It's quick & easy.

I need to display an array of structures on screen of an unknown length

P: n/a
Hello,
We've been given a program that reads data from a .dat file. It looks
something like this:

1 1.0 1.2
2 2.4 3.5
3 3.4 6.5
4 3.3 2.2

....and so on. I need to know a way of preinting this on screen - i can
do this easily for a file of known size, i have a statement like:

fprintf(stdout, "%d %lf %lf", node_array[no_nodes-1].id,
node_array[no_nodes-1].x, node_array[no_nodes-1].y);

where i can replace the "-1" with "-n". However, seeing as the arrays
are of a length anywhere between 1 and 100 lines long, well, theres
got to be a better way of doing it! Otherwise, any array less than 100
structures long will have a lot of rubbish printed out. Oh course you
knew that - I'm very new at C, so I'm being obvious for my own
understanding!

Any help would be very much appreciated!

James Brennan - Craddock
Nov 13 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
1. I assume you're allocating memory for 100 elements as that's the max
you're expecting.

2. You can check if there are no more elements by reading like this:
while(fscanf("%d %lf %lf", &id, &x, &y) != EOF)
fprintf(....)

If the end of file is reached the while loop exits.

James wrote:
Hello,
We've been given a program that reads data from a .dat file. It looks
something like this:

1 1.0 1.2
2 2.4 3.5
3 3.4 6.5
4 3.3 2.2

...and so on. I need to know a way of preinting this on screen - i can
do this easily for a file of known size, i have a statement like:

fprintf(stdout, "%d %lf %lf", node_array[no_nodes-1].id,
node_array[no_nodes-1].x, node_array[no_nodes-1].y);

where i can replace the "-1" with "-n". However, seeing as the arrays
are of a length anywhere between 1 and 100 lines long, well, theres
got to be a better way of doing it! Otherwise, any array less than 100
structures long will have a lot of rubbish printed out. Oh course you
knew that - I'm very new at C, so I'm being obvious for my own
understanding!

Any help would be very much appreciated!

James Brennan - Craddock


Nov 13 '05 #2

P: n/a
ji****@hotmail.com (James) wrote:
Hello,
We've been given a program that reads data from a .dat file. It looks
something like this:

1 1.0 1.2
2 2.4 3.5
3 3.4 6.5
4 3.3 2.2

...and so on. I need to know a way of preinting this on screen - i can
do this easily for a file of known size, i have a statement like:

fprintf(stdout, "%d %lf %lf", node_array[no_nodes-1].id,
node_array[no_nodes-1].x, node_array[no_nodes-1].y);

where i can replace the "-1" with "-n". However, seeing as the arrays
are of a length anywhere between 1 and 100 lines long, well, theres
got to be a better way of doing it! Otherwise, any array less than 100
structures long will have a lot of rubbish printed out. Oh course you
knew that - I'm very new at C, so I'm being obvious for my own
understanding!

Any help would be very much appreciated!


Two possible approaches spring to mind:

1. Count the number of records/lines when you read them in, so
you know how many to print afterwards.

2. Use a sentinel, i.e an indicator value that tells you when
no more valid data follows.

HTH a bit.

Regards
--
Irrwahn
(ir*******@freenet.de)
Nov 13 '05 #3

P: n/a
Pushkar Pradhan wrote:

<top-post snipped>

This is the second time you've been asked. Please stop top-posting.

-Kevin
--
My email address is valid, but changes periodically.
To contact me please use the address from a recent posting.

Nov 13 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.