我无法用 C 语言打开文件,即使该文件存在并且未被任何应用程序使用。谁能告诉我是什么导致了这个问题?
int main()
{
FILE* oud;
unsigned size;
unsigned* bytes;
char path[] = "C:\\Users\\Ruben\\Documents\\test.txt";
errno_t error;
if ((error = fopen_s(&oud, path, "rb" )) == NULL)
{
perror(NULL);
getchar();
return -1;
}
fclose(oud);
getchar();
return 0;
}
}
输出是:“没有错误”。
最佳答案
fopen_s()
成功时返回 0
,失败时不返回 NULL
:
Zero if successful; an error code on failure. See _doserrno, errno, _sys_errlist, and _sys_nerr for more information on these, and other, error codes.
NULL
宏是 #define
d 到 0
(可能),这意味着如果文件打开 if()
在发布的代码中是:
if (0 == 0)
这显然是正确的。更改为:
if ((error = fopen_s(&oud, path, "rb" )) != 0)
关于c - 无法在 C 中打开文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12897396/