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.
program file.txtor read from stdin in your code.fp = fopen(argv[1], "r"); fscanf(fp, "%lf", values);How do you knowfp != 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.