【发布时间】:2020-08-05 16:45:04
【问题描述】:
我有这个代码:
FILE *setup=fopen(strcat(cwd,"setup.txt"),"r");//navigates up to setup file
...//(doing stuff with setup)
fclose(setup);
FILE *paths=fopen(strcat(cwd,"stuff.txt"),"r");
char buff2[1024];
fgets(buff2,200,paths);
char thing[1024];
strcpy(thing,buff2);
printf(thing);
fclose(paths);
但它的执行方式不同,具体取决于我是先使用 setup 代码还是先使用路径代码 - 只有第一个可以正常工作,第二个只会在调用 fgets 时产生乱码。 fclose 应返回 0。我查看第二个文件的具体方式似乎并没有影响它。这里发生了什么?
【问题讨论】:
-
发布Minimal, Reproducible Example。检查
fopen()s 是否成功。strcat()会修改cwd的内容。您是否在每个strcat()之前正确初始化cwd?还要注意printf(thing);是危险的,因为thing可能包含%。