0
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
5
  • 4
    Welcome to SO. You do not check whether you got a valid FILE*. If you run your program from wrong directoy you may not find the file and then try to use a NULL pointer with feof and fscanf. You should also always check the return value of fscanf and other IO functions. Commented Jan 30, 2023 at 10:42
  • Did you run your program in a debugger? It should tell you where you get the segmentation fault and then you can inspect what values are stored in the involved variables. Commented Jan 30, 2023 at 10:43
  • 1
    while(feof(matrix_r)) would usually be while(!feof(matrix_r)) but even that is wrong. Use the value returned by fscanf to control the loop. while(fscanf(matrix_r, %lf%c", &x, &c) == 2) Commented Jan 30, 2023 at 10:45
  • 1
    If the intention was while(!feof(matrix_r)): stackoverflow.com/questions/5431941/…. Commented Jan 30, 2023 at 10:46
  • Note that while (!feof(file)) is always wrong! Commented Jan 30, 2023 at 14:05

2 Answers 2

2

fopen() failed and feof(NULL) caused the segfault. If fopen() was successful then feof() would return false and the loop wouldn't run but your program wouldn't segfault.

Check the return value of fopen() & fscanf(). You only need to call feof() if you need to find out why fscanf() failed to read 2 items.

#include <stdio.h>

int main() {
    FILE *matrix_r = fopen("matrix.txt", "r");
    if(!matrix_r) {
        perror("fopen");
        return 1;
    }
    for(;;) {
        double x;
        char c;
        int rv = fscanf(matrix_r, "%lf%c", &x, &c);
        if(rv != 2)
            break;
        printf("%lf%c", x, c);
    }
    fclose(matrix_r);
}

Here is the output:

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
Sign up to request clarification or add additional context in comments.

4 Comments

It will not work as it will fail at the end of the first line.
@0___________ Why do you think that? It works for me.
It works for me. Did it? Was the final 0.00000 printed?
@AndrewHenle Yes. I updated answer with output (which is identical to input). What's the concern?
1
  1. You do not check if fopen was successful. Calls to feof or fscanf if the file pointer is NULL invoke Undefined Behaviour

  2. while(feof(matrix_r)){

while(!feof(...))) is always wrong (Why is “while( !feof(file) )” always wrong?), but your one has no logical sense at all (as you want to scanf if it is the end of the file).

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.