0

hello,

I have a rather old system, running Red Hat 5, and I need to write a program that, among other things, imports data from an external file(s) and writes it into array(s); the following is the format of my external data file (obviously it's much larger):

2.59    3.17    4.18    2.21
5.19    2.87    5.14    3.66
9.27    4.18    3.16    5.25

In this case I would need to create an array with 4 columns and 3 rows. However, the size of the array cannot be explicitly specified, it has to be dynamically allocated based on the data in the external file.

I thought about using the array.Length to determine the length of the imported data, but I'm not sure how to import it and how to separate it into array elements. Thanks for any help.

r.

2
Contributors
1
Reply
3
Views
8 Years
Discussion Span
Last Post by Salem
0

Use fgets() to read each line into a buffer.
Use strtod() to convert each float, and advance a pointer through the buffer.

A simple way would be to read the whole file just to determine the dimensions of the array you would require, then call malloc to allocate all the space you need. Then you would read AND convert the second time through.

A more complicated approach would be to use realloc to allocate as you go, and only have to read the file in one pass.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.