c - 无法在 C 中打开文件

标签 c windows file fopen

我无法用 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 宏是 #defined 到 0(可能),这意味着如果文件打开 if() 在发布的代码中是:

if (0 == 0)

这显然是正确的。更改为:

if ((error = fopen_s(&oud, path, "rb" )) != 0)

关于c - 无法在 C 中打开文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12897396/

相关文章:

mongodb - Golang 更新 mongodb 中的现有文件

c++ - 用于字节偏移量操作的更清晰的指针算术语法

c - 将工资分成最小金额的美国钞票

"fork()"生成的子进程的进程 ID 是否可以小于其父进程?

c - 使用 C 中的函数初始化数组

windows - 在Windows中配置HBase群集

java - Windows 和 Linux 中用于 Java 代码的路径定界符

c++ - 为什么有些函数需要作为DLL文件注入(inject)才能工作

python - 逐行读取文件还是使用 read() 方法?

c - 从文件中存储多种类型的数据