简体   繁体   中英

Xcode: EXC_BAD_ACCESS when using fgetc() in C

I am trying to load a map for a game from a txt file, but Xcode gives me this error 'EXC_BAD_ACCESS' when I try to read from a file using fgetc() function.

I tried to do the same in another IDE and it works perfectly fine, but Xcode keeps giving me this error. I even tried fscanf(), but unsuccessful.

Here is the code:

FILE* file = fopen("res/txt/map_level_1.txt", "r");
if (file == NULL) {
    printf("Could not open the file!\n");
    return;
}

char c = fgetc(file); // error EXC_BAD_ACCESS
printf("%c", c);

fclose(file);

Help me to solve the problem.

Thank you in advance.

Try to specify your working directory to your file location.

On the menu bar, select Product -> Scheme -> Edit Scheme , then Run -> Options . Under Working Directory , enable Custom Working Directory and choose your file location.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM