C fread char 代码块

C fread char Codeblocks

我正在尝试 运行 使用 Codeblocks 16.11 的简单程序。我在尝试从以下代码中的文件中读取字符时遇到问题...

FILE *fo;
FILE *ft;
char c;

if ((fo = fopen("mayus.txt", "r")) == NULL){
        perror("opening mayus");
    }

int m;
m= fread(c, 1, 1, fo);
printf("I just read for the first time with result m = %d\n",m);

我的文件 mayus.txt 它只是一个文本文件,里面写着 "AbCDEFGHIjK"。我期待看到 m=1 的 printf,但我一直在控制台中看到 m=0。

首先检查fread原型:

size_t fread(void *ptr, size_t size, size_t nmemb, FILE *stream);

所以m= fread(c, 1, 1, fo);是错误的

你需要的是

m = fread(&c, 1, 1, fo);  // the first param should be a pointer to the buffer