0

I am trying to read from a file specified in a command prompt through terminal using the line program < file.txt and then print it again to check it works. I get the error Segmentation fault: 11, I'm not sure if my file is opening correctly in my program.

This is the code so far:

#define MAX 1000
int
main(int argc, char *argv[]) {
    FILE *fp;
    double values[MAX];

    fp = fopen(argv[1], "r");
    fscanf(fp, "%lf", values);
    printf("%f\n", *values);
    fclose(fp);

    return 0;
}

Any help or feedback would be greatly appreciated.

4
  • 2
    Either invoke your program with program file.txt or read from stdin in your code. Commented Apr 26, 2014 at 13:00
  • 1
    fp = fopen(argv[1], "r"); fscanf(fp, "%lf", values); How do you know fp != NULL? That's a condition that could cause a segfault (and there don't appear to be other conditions in your code that could). If you're able to duplicate a failure with such a small block of code, you really should try to debug it yourself before asking for help! Not only does it reduce site clutter, but you will learn much more like that and become a better developer as a result. Commented Apr 26, 2014 at 13:05
  • use valgrind to know which line raises the segmentation fault. Commented Apr 26, 2014 at 13:39
  • 1
    @ValentinLorentz Telling someone who is obviously not used to a command line interface to use valgrind or gdb is sort of ironic, isn't it? Commented Apr 26, 2014 at 14:07

3 Answers 3

5

You should execute your program like

./program  file.txt
Sign up to request clarification or add additional context in comments.

Comments

1

I'm not sure if my file is opening correctly in my program

Then you should really test for it, you are getting a segfault because fopen is returning NULL.

#include <stdio.h>

#define MAX 1000
int
main(int argc, char *argv[]) {
    FILE *fp;
    double values[MAX];

    fp = fopen(argv[1], "r");
    if (!fp) {
        printf("Invalid file name \n");
        return -1;
    }

    fscanf(fp, "%lf", values);
    printf("%f\n", *values);
    fclose(fp);

    return 0;
}

fopen is NULL because you are invoking the program in the wrong manner, < and > are a re-directions which can be useful but is not what you are trying to do in this case, correct way to invoke it is to simply pass it the arguments directly.

./program input.file

4 Comments

Fair answer, and indeed it will tell the OP that the file could not be opened. Which would automatically lead to a next question. Better to also add why the OP's program < file.txt failed.
@Jongware I was looking for a SO answer going into bash redirections in order to explain what < means in this context but seems beyond the scope of the question
Agreed with that, hence my assessment it would irrevocably lead to a follow-up question: "why does it tell me the file cannot be opened even though I supply it on the command line?" Your addition is useful in this regard.
By the way: I downvoted the question because the problem is not in the code, but in the OP's misunderstanding of pretty much basic command line operations.
0

Yeah, either: 1) check the way you're invoking it, i.e,

  • check if the 'program' is an executable file, you can make it executable using chmod command in linux
  • check if the path to 'program' or 'file.txt' is correct

2) (I'm not sure of this): check if the content of 'file.txt' is of the right content. (I don't think it should affect to the extent that it causes a segmentation fault, but still, check it.)

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.