这是我的代码:
#include <stdio.h>
#include <stdlib.h>
const char FILE_NAME[] = "inputfile.txt";
int main()
{
FILE *in_file; /* input file */
in_file = fopen(FILE_NAME, "r");
if (in_file == NULL) {
printf("Cannot open %s\n", FILE_NAME);
exit(8);
}else{
printf("File opened %s\n", FILE_NAME);
}
fclose(in_file);
return (0);
}
我收到此错误:
Cannot open inputfile.txt
但是文件就在那里。 有人可以帮助我吗?
(如果我将“r”更改为“w”,我可以写入该文件,但我在 SSD 上找不到该文件)
最佳答案
程序将在当前工作目录中查找该文件。这很可能与已编译的可执行文件位于同一目录。
如果您使用的是 IDE,请检查项目设置并检查已编译的可执行文件的放置位置。然后手动将文本文件复制到该目录或编辑项目,以便构建它时自动复制文件。
如果您从命令行工作(假设您的编译器名为 gcc
):
cd ~/Documents/programming/c/PracticalC/chapter14
gcc -Wall ch14pexercise1.c -o prog
./prog
关于c - 如何使用文件结构?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32010990/