为什么当我尝试读取文件时文件变为空?

问题描述 投票:-1回答:1

我正在尝试读取项目文件夹中存在的文件。但是,当我想在其中获取字符串时,文件的数据更改为(null)。我尝试了“ w”和“ r”,但没有一个起作用。这是我的代码:

    FILE* f;
    if (!(f =fopen("a.txt", "r"))) {printf("Could not open file\n"); return;}
    char s[10000];
    fgets(s, 10000, f);
    printf("%s", s);

输出:

(null)
  • 注意:打开文件没有错误

here is the file after opening

编辑:这是我的代码的完整版本:

    FILE* channelfile;
    char filename[100] , name[]="hi";
    sprintf(filename, "./Resources/Channels/%s.cyko", name);

    if (!(channelfile =fopen("hi.cyko", "r"))) {printf("Could not open file\n"); return;}
    char s[10000];
    fgets(s, 10000, channelfile);
    printf("%s", s);

cyko是我的文件类型(不通用)

the file destinationthe original text file

c file fopen fgets
1个回答
3
投票

该文件包含文本(null),如屏幕转储所示。

结果,打印文件的内容会打印(null)

QED。

© www.soinside.com 2019 - 2024. All rights reserved.