My file looks like:
123456789
My code gives me segmentation fault:
#include <stdio.h>
int main(){
FILE *f;
char ch[5];
f = open("a.txt", "r");
fgets( ch, 4, f);
ch[4] = NULL;
printf("%s", ch); //Fixed
return 0;
}
I am an absolute beginner. What am I doing wrong. My aim is to read first 4 characters of the file using fgets
.