简体   繁体   English

Xcode:在C中使用fgetc()时,EXC_BAD_ACCESS

[英]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. 我正在尝试从txt文件加载游戏地图,但是当我尝试使用fgetc()函数从文件中读取内容时,Xcode给我这个错误'EXC_BAD_ACCESS'。

I tried to do the same in another IDE and it works perfectly fine, but Xcode keeps giving me this error. 我试图在另一个IDE中做同样的事情,它工作得很好,但是Xcode一直给我这个错误。 I even tried fscanf(), but unsuccessful. 我什至尝试了fscanf(),但没有成功。

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 . 在菜单栏上,选择Product -> Scheme -> Edit Scheme ,然后选择Run -> Options Under Working Directory , enable Custom Working Directory and choose your file location. 在“ Working Directory ,启用“ Custom Working Directory然后选择文件位置。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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