我正在尝试将相对路径传递给 fopen(),但它似乎无法找到该文件。我需要这个才能在 Linux 上工作。 文件名(例如:t1.txt)保存在一个数组中。所以我需要的只是相对路径的“前面部分”。
这是我的代码:
// Use strcat to create a relative file path
char path[] = "./textfiles/"; // The "front part" of a relative path
strcat( path, array[0] ); // array[0] = t1.txt
// Open the file
FILE *in;
in = fopen( "path", " r " );
if (!in1)
{
printf("Failed to open text file\n");
exit(1);
}
最佳答案
in = fopen("path", " r ");
这里有两个错误:
- 您不想打开字面上命名为“路径”的文件,您想要的是名称在变量
path
中的文件
- 模式参数无效;你想要
"r"
让他们做对
in = fopen(path, "r");
关于c - 将相对路径传递给 fopen(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9120842/