gpt4 book ai didi

c - 从文件中读取。 C

转载 作者:太空宇宙 更新时间:2023-11-04 03:31:54 25 4
gpt4 key购买 nike

我想用 fgetc() 读取这个符号(我不能复制它,这里 link to file.txt )

此文件长 2 个字节,其二进制转储为 1A 98

fgetc() 无法读取并返回 -1。请帮忙((

for (int k = 0; k < fileSize; k++)
{
buffer[k] = (unsigned char) fgetc(f);
}

这个符号的图片:
Picture of this symbols

也许有字符编码?

最佳答案

您的文件包含0x1A,这意味着以文本模式打开文件时,EOF 和读取将停止读取。

尝试以二进制模式打开文件。

这是一个测试代码:

#include <stdio.h>

int main(void) {
const char *fileName = "codeText.txt";
FILE* fp;
int input;

fp = fopen(fileName, "r");
if (fp==NULL) return 1;
puts("text mode:");
while((input = getc(fp)) != EOF) printf("%02X\n", (unsigned int)input);
fclose(fp);

fp = fopen(fileName, "rb");
if (fp == NULL) return 1;
puts("binary mode:");
while((input = getc(fp)) != EOF) printf("%02X\n", (unsigned int)input);
fclose(fp);

return 0;
}

关于c - 从文件中读取。 C,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35796841/

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