How to read numerical data from a file

W

Wei-Chao Hsu

There are some data files look like

1.) data1.txt
----------------
1.0 2.0 3.0
4.0 5.0 6.0
7.0 8.0 9.0
10.0

2.) data2.txt
---------------------
1.0 2.0 3.0 4.0
5.0 6.0 7.0 8.0
9.0 10.0

3.) data3.txt
----------------
1.0 2.0
3.0 4.0 5.0
6.0 7.0
8.0 9.0 10.0

They will be store in a array. A[0]=1.0,A[1]=2.0,.....
I never know the format in each file.
In fortran, I just declare an array A(10), and read them
READ(file_no,*)A
I don't know how to do the same thing in C.
I need your help. Thanks!

Dennis
 
E

Emmanuel Delahaye

Wei-Chao Hsu said:
There are some data files look like

1.) data1.txt
----------------
1.0 2.0 3.0
4.0 5.0 6.0
7.0 8.0 9.0
10.0

I'm not going to give you a working solution, but here are some hints:

A smart combination of fgets() (reads a line of text) and strtod()
(converts text to double) should work. Read carefully your C-book about the
parameters and behaviour of these functions.
 
J

Jens.Toerring

Wei-Chao Hsu said:
There are some data files look like
1.) data1.txt
----------------
1.0 2.0 3.0
4.0 5.0 6.0
7.0 8.0 9.0
10.0
2.) data2.txt
3.) data3.txt
----------------
1.0 2.0
3.0 4.0 5.0
6.0 7.0
8.0 9.0 10.0
They will be store in a array. A[0]=1.0,A[1]=2.0,.....
I never know the format in each file.
In fortran, I just declare an array A(10), and read them
READ(file_no,*)A
I don't know how to do the same thing in C.

Without any error checking (which you definitely should do) and
assuming that the lines of dashes don't belong to the files:

int i;
double A[ 10 ];
FILE *fp = fopen( "data1.txt", "r" );
for ( i = 0; i < 10; i++ )
fscanf( fp, "%lf", A + i );

Hint: scanf() skips white space in most cases (that includes the end
of line, i.e. '\n').
Regards, Jens
 
A

Al Bowers

Wei-Chao Hsu said:
There are some data files look like

1.) data1.txt
----------------
1.0 2.0 3.0
4.0 5.0 6.0
7.0 8.0 9.0
10.0

2.) data2.txt
---------------------
1.0 2.0 3.0 4.0
5.0 6.0 7.0 8.0
9.0 10.0

3.) data3.txt
----------------
1.0 2.0
3.0 4.0 5.0
6.0 7.0
8.0 9.0 10.0

They will be store in a array. A[0]=1.0,A[1]=2.0,.....
I never know the format in each file.
In fortran, I just declare an array A(10), and read them
READ(file_no,*)A
I don't know how to do the same thing in C.
I need your help. Thanks!

If the data is as shown in the above examples, all floating
point numbers, you can use function fscanf to read the file.

To declare an array:
double array[100];
where the number is large enough to hold the values from the file.

Declare a variable to hold a count of the data read from the file.
unsigned count;

To open the file for reading, you use function fopen.
FILE *fp = fopen("test.txt","r");

Use function fscanf in a loop to read the file and store the file
contents in the array.
if(fp)
for(count = 0; count < 100 &&
1 == fscanf(fp,"%lf",&array[count]); count++) ;

then close the file.
fclose(fp);
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,262
Messages
2,571,043
Members
48,769
Latest member
Clifft

Latest Threads

Top