Reputation: 3
I have a text file of 257 points like this
3.78135
2.84681
2.81403
2.54225
3.10854
...
and I would like to read this data and copy them into an array. With the help of similar answered question I wrote this:
#include<stdio.h>
#include<stdlib.h>
int max_read = 258;
double phi[max_read];
FILE *stream;
stream = fopen("namefile.txt", "r");
if (stream == NULL) {
print ("! Cannot open file %sn", "namefile.txt\n");
exit(1);
} else{
int m = 0;
while(m<max_read) {
phi[m] = // But I still don't know how write the correct value into the array.
m++;
}
}
I also would like to do this reading-coping procedure until the end of file.
Upvotes: 0
Views: 988
Reputation: 312
This should do the trick i think.
if (stream == NULL) {
fprint("! Cannot open file %sn", "namefile.txt\n");
exit(1);
} else{
int m = 0;
while (fscanf(stream, "%lf\n", &phi[m])){
m++;
}
}
Upvotes: 4