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

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

问题描述

我一直在试图打开一个文件,输出文本,但我不断收到错误。所以我想我会开始在开始的时候,只是尝试打开该文件。这是我的code:

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(错误); ,你应该尝试 PERROR (错误)可打印故障的实际原因(如权限问题,无效的参数,等等)。

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天全站免登陆