C fopen() - 绝对路径可能存在的问题
C fopen() - Possible issue with absolute path
我目前在 Windows 10.
下使用 Code::Blocks 13.12(编译器:GNU GCC)
我正在尝试打开文件并加载其内容,但 fopen 给我带来了麻烦。 'input.txt' 与我的可执行文件位于同一目录中。我已经检查了权限。
获取路径的函数:
char* getFileName()
{
char *fileName; /* the path of the .txt file */
char path[MAX_PATH];
/* get the path of the executable */
GetModuleFileName(NULL, path, MAX_PATH);
/* remove the name of the executable from the path */
PathRemoveFileSpec(path);
/* check case where path is directory root */
if(PathIsRoot(path))
strcat(path, "\*");
/* add the name of the .txt file to the path */
strcat(path, "\input.txt");
/* not sure if needed */
path[strlen(path)] = '[=10=]';
fileName = strdup((char *) path);
return fileName;
}
加载文件内容的函数:
bool loadDict(char *fileName)
{
FILE *fp; /* file stream */
char line[LINE_SIZE]; /* each line of the file */
// other variables
/* try to open file for reading */
if((fp = fopen(fileName, "r")) == NULL)
{
fprintf(stderr, "Error: Could not open the file '%s' to read\n", fileName);
return false;
}
// stuff done
/* file is no longer needed, close it */
if(fclose(fp))
{
fprintf(stderr, "Error: Could not close the file '%s' to read\n", fileName);
return false;
}
return true; /* in case no problem has occured */
}
主要:
int main()
{
char *fileName;
fileName = getFileName();
/* try to load the dictionary into memory */
if(!loadDict(fileName))
{
fprintf(stderr, "Error: The dictionary could be not loaded into memory.\nProgram terminating...\n");
return 1;
}
// other stuff
return 0;
}
我遇到了两个错误(无法打开文件,无法加载)。我已经尝试将“\”替换为“/”或使用双斜杠,但均未成功。
FILE *fp = fopen("path\input.txt", "r");
如有任何帮助,我们将不胜感激。
您正在 return 获取 getFileName
中局部变量的地址,这会导致 未定义的行为 。
这是 C 中的一个常见陷阱。
您需要:
A) 在堆上分配字符串(使用例如 malloc
)和 return 它。
B) 有 getFileName
获取指向 caller-allocated 缓冲区的指针,然后填充该缓冲区。
此外,在调试此类问题时,不要假设一切正常。在尝试 fopen
之前,请使用 printf
查看 filename
的值。
您的数组 path
是一个局部变量,其作用域仅限于函数 getFileName
。不要return它的地址。
而是从调用函数传递它。
我目前在 Windows 10.
下使用 Code::Blocks 13.12(编译器:GNU GCC)我正在尝试打开文件并加载其内容,但 fopen 给我带来了麻烦。 'input.txt' 与我的可执行文件位于同一目录中。我已经检查了权限。
获取路径的函数:
char* getFileName()
{
char *fileName; /* the path of the .txt file */
char path[MAX_PATH];
/* get the path of the executable */
GetModuleFileName(NULL, path, MAX_PATH);
/* remove the name of the executable from the path */
PathRemoveFileSpec(path);
/* check case where path is directory root */
if(PathIsRoot(path))
strcat(path, "\*");
/* add the name of the .txt file to the path */
strcat(path, "\input.txt");
/* not sure if needed */
path[strlen(path)] = '[=10=]';
fileName = strdup((char *) path);
return fileName;
}
加载文件内容的函数:
bool loadDict(char *fileName)
{
FILE *fp; /* file stream */
char line[LINE_SIZE]; /* each line of the file */
// other variables
/* try to open file for reading */
if((fp = fopen(fileName, "r")) == NULL)
{
fprintf(stderr, "Error: Could not open the file '%s' to read\n", fileName);
return false;
}
// stuff done
/* file is no longer needed, close it */
if(fclose(fp))
{
fprintf(stderr, "Error: Could not close the file '%s' to read\n", fileName);
return false;
}
return true; /* in case no problem has occured */
}
主要:
int main()
{
char *fileName;
fileName = getFileName();
/* try to load the dictionary into memory */
if(!loadDict(fileName))
{
fprintf(stderr, "Error: The dictionary could be not loaded into memory.\nProgram terminating...\n");
return 1;
}
// other stuff
return 0;
}
我遇到了两个错误(无法打开文件,无法加载)。我已经尝试将“\”替换为“/”或使用双斜杠,但均未成功。
FILE *fp = fopen("path\input.txt", "r");
如有任何帮助,我们将不胜感激。
您正在 return 获取 getFileName
中局部变量的地址,这会导致 未定义的行为 。
这是 C 中的一个常见陷阱。
您需要:
A) 在堆上分配字符串(使用例如 malloc
)和 return 它。
B) 有 getFileName
获取指向 caller-allocated 缓冲区的指针,然后填充该缓冲区。
此外,在调试此类问题时,不要假设一切正常。在尝试 fopen
之前,请使用 printf
查看 filename
的值。
您的数组 path
是一个局部变量,其作用域仅限于函数 getFileName
。不要return它的地址。
而是从调用函数传递它。