我在使用 fgetc 函数从文件中读取值时遇到问题。这是我正在使用的代码:
#include <stdio.h>
#include <stdlib.h>
int main()
{
int k;
FILE* rede = fopen("/home/leocal/Desktop/Poli/2018-1/Numérico/EP1/Redes/1_Stevenson/a.txt", "r");
if (rede == NULL) {
printf("Couldn't open file\n");
return 1;
}
k = fgetc(rede);
if (k!=EOF) {
printf("printing k : %d",k);
}
fclose(rede);
return 0;
}
文件“a.txt”只有一个字符“1”,但 k
收到值 49。
最佳答案
fgetc()
的描述说:
Returns the character currently pointed by the internal file position indicator
您正在读取字符“1
”,然后打印它的十进制值
printf("printing k : %d",k);
要使用 printf
打印字符的值,请使用 %c
:
printf("printing k : %c",k);
您可以阅读有关 printf
以及如何格式化其输出的更多信息 here .
关于c - fgetc 在预期为 1 时返回意外值 49,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50161018/