Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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).

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading