I'm not sure why this is happening but I am getting a "Segmentation fault (core dumped)" from this very simple code. Any ideas as to why? I have to use a string to tell fopen() what file to open.
#include <stdio.h>
#include <string.h>
int main(void) {
char *small = "small.ppm";
FILE * fp;
char word[5];
fp = fopen(small, "r");
fscanf(fp, "%s", word);
printf("%s\n", word);
return 0;
}
Your code could invoke undefined behavior, replace by:
#include <stdio.h>
#include <stdlib.h>
int main(void) {
char *small = "small.ppm";
FILE * fp = fopen(small, "r");
if (fp == NULL) {
perror("fopen()");
return EXIT_FAILURE;
}
char word[5];
if (fscanf(fp, "%4s", word) != 1) {
fprintf(stderr, "Error parsing\n");
return EXIT_FAILURE;
}
printf("%s\n", word);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With