【发布时间】:2019-10-12 22:23:39
【问题描述】:
这个函数被传递了一个文本文件的路径(mapper_path),它包含了每一行其他文本文件的路径。我应该打开 mapper_path.txt 文件,然后打开并评估其中的每个路径(输出中的示例)。 fopen 在 mapper_path 文件上成功,但在它包含的路径上失败。 在失败的情况下,它会打印出我试图打开的确切路径。 我在 Windows 上使用 C 语言并在 Ubuntu 子系统上运行命令。
如何正确读取子路径并将其存储到变量中以打开它?
根据 Rici 的建议解决了!
int processText(char * mapper_path, tuple * letters[])
{
char line[LINE_SIZE];
char txt_path[MAX_PATH];
FILE * mapper_fp = fopen(mapper_path, "r");
if(!mapper_fp)
{
printf("Failed to open mapper path: %s \n", mapper_path);
return -1;
}
//!!! PROBLEM IS HERE !!!
while(fgets(txt_path, MAX_PATH, mapper_fp))
{
//remove newline character from end
txt_path[strlen(txt_path)-1] = 0;
//open each txt file path, return -1 if it fails
FILE* fp = fopen(txt_path, "r");
if(!fp)
{
printf("Failed to open file path:%s\n", txt_path);
return -1;
}
//...more unimportant code
打印: 无法打开文件路径: /mnt/c/users/adam/documents/csci_4061/projects/blackbeards/testtext.txt 这是我要打开的文件的确切路径。
【问题讨论】:
-
这种情况一般发生在文件路径不存在,或者你没有打开文件的权限