无法使用 fopen() 打开文件 [英] Unable to open a file with fopen()

查看:36
本文介绍了无法使用 fopen() 打开文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在尝试打开一个文件并输出文本,但我一直收到错误消息.所以我想我会从一开始就尝试打开文件.这是我的代码:

I've been trying to open a file and output text, but I keep getting errors. So I thought I would start at the very beginning and just try opening the file. This is my code:

#include <stdio.h>
#include <stdlib.h>
#define CORRECT_PARAMETERS 3

int main(void)
{
    FILE *file;
    file = fopen("TestFile1.txt", "r");
    if (file == NULL) {
        printf("Error");
    }
    fclose(file);
}

当我运行文件时,错误"被打印到控制台,就是这样.TestFile1.txt 与我的 .exe 位于同一位置.我该如何解决?

When I run the file, "Error" gets printed to the console and that's it. The TestFile1.txt is in the same location as my .exe. How do I fix this?

推荐答案

代替printf("Error");,你应该试试perror("Error")这可能会打印失败的实际原因(如权限问题、无效参数等).

Instead of printf("Error");, you should try perror("Error") which may print the actual reason of failure (like Permission Problem, Invalid Argument, etc).

这篇关于无法使用 fopen() 打开文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆