gpt4 book ai didi

c - 为什么在 C 中输入为 0 时我的 getc() 总是返回 30?

转载 作者:太空宇宙 更新时间:2023-11-04 08:45:51 24 4
gpt4 key购买 nike

int main(void){
char buffer[5] = {0};
int i;
FILE *fp = fopen("haha.txt", "r");
if (fp == NULL) {
perror("Failed to open file \"mhaha\"");
return EXIT_FAILURE;
}
for (i = 0; i < 5; i++) {
int rc = getc(fp);
if (rc == EOF) {
fputs("An error occurred while reading the file.\n", stderr);
return EXIT_FAILURE;
}
buffer[i] = rc;
}
fclose(fp);
printf("The bytes read were... %x %x %x %x %x\n", buffer[0], buffer[1], buffer[2], buffer[3], buffer[4]);
return EXIT_SUCCESS;
}

我在我的 haha​​.txt 文件中放了八个 0,当我运行这段代码时,它总是给我:

The bytes read were... 30 30 30 30 30

谁能告诉我为什么?

最佳答案

因为 '0' == 0x30

字符“0”是 0x30 (ascii)。

关于c - 为什么在 C 中输入为 0 时我的 getc() 总是返回 30?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21689438/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com