我正在关注tutorialspoint.com上的基本C编程教程
我有以下程序在/ tmp中生成一个名为test.txt的文件:
#include <stdio.h>
int main() {
FILE *fp;
fp = fopen("/tmp/test.text", "w+");
fprintf(fp, "This is testing for fprintf...\n");
fputs("This is testing for fputs...\n", fp);
fclose(fp);
return 0;
}
然后我有第二个程序,它只是试图打开该文件进行阅读:
#include <stdio.h>
#include <string.h>
#include <errno.h>
int main() {
FILE *fp = NULL;
fp = fopen("/tmp/test.txt", "r");
if (fp == NULL) {
printf("NULL!!!\n");
}
printf("%s\n", strerror(errno));
fclose(fp);
return 0;
}
但是,当我尝试运行打开文件的程序时,我得到以下输出:
NULL!!!
No such file or directory
Segmentation fault
如果我修改代码指向我的主目录中的同一文件,它可以正常工作 . 看来,由于某些原因,我无法在/ tmp目录中打开文件(通过fopen)...而且为了清楚起见,我可以更改为/ tmp并cat了test.txt的内容文件很好 . 权限看起来也正常,664我的用户作为所有者和组 .
我可以想到的唯一可能与我的系统有关的其他细节是我在Elementary OS Juno上,我使用的是7.3.0(clang也给出相同的结果),并且我有单独的加密分区操作系统根和家...
有什么可能导致这个问题的想法?
1 回答
这只是由@yano指出的不正确的文件扩展名,.txt vs.text引起的