int main() {
FILE *matrix_r;
matrix_r=fopen("matrix.txt","r");
double x;char c;
while(feof(matrix_r)){
fscanf(matrix_r,"%lf%c",&x,&c);
printf("%lf%c",x,c);
}
fclose(matrix_r);
return 0;
}
Trying to read float values from the file but getting segmentation fault core dumped error. matrix.txt stores a matrix of floats.
contents of matrix.txt are below.
0.000000,876.671546,448.879717,1349.827396
876.671546,0.000000,1319.195209,964.193445
448.879717,1319.195209,0.000000,1741.628261
1349.827396,964.193445,1741.628261,0.000000
FILE*. If you run your program from wrong directoy you may not find the file and then try to use aNULLpointer withfeofandfscanf. You should also always check the return value offscanfand other IO functions.while(feof(matrix_r))would usually bewhile(!feof(matrix_r))but even that is wrong. Use the value returned byfscanfto control the loop.while(fscanf(matrix_r, %lf%c", &x, &c) == 2)while(!feof(matrix_r)): stackoverflow.com/questions/5431941/….while (!feof(file))is always wrong!