Search code examples
csegmentation-faultscanf

segmentation fault error when reading a file in C


I was trying to read a file char by char but the debugger when reaches the fscanf gives back a segmentation fault error, here's my code:

int main(){
FILE *inFile;
char *carattere = NULL;

inFile = fopen("../file.txt", "r");
if (inFile == NULL){    
    return -1;
}

while(fscanf(inFile, "%c", carattere) != EOF){  //segmentation fault
    printf("%c ", *carattere);
}
fclose(inFile);
return 0;}

(I've recentely reinstalled my IDE).


Solution

  • You should provide some space for your character to be read. Where does carattere point to when you try to write into it?