gpt4 book ai didi

c - 从c中的文件中读取所有字节

转载 作者:可可西里 更新时间:2023-11-01 10:48:52 26 4
gpt4 key购买 nike

我正在尝试从文件中读取字节,但大小已损坏。调试时我可以看到 st.st_size 的值,但我无法读取 size 的值。

int main(void)
{
FILE* oud;
struct stat st;
_off_t size;
unsigned* bytes;
char const path[] = "C:\\Users\\Ruben\\Documents\\test.txt";
errno_t error;

if ((error = fopen_s(&oud, path, "rb" )) != NULL)
return -1;

stat(path, &st);
size = st.st_size;
bytes = (unsigned*)malloc(size);
fread(bytes, 1, size, oud);
fclose(oud);

getchar();
return 0;
}

最佳答案

我建议使用循环并逐字节遍历文件字节,检查 EOF 字符。一旦找到它,您现在就知道了文件的实际大小,并且可以从一开始就适本地读取它,将其放入缓冲区或其他地方。

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

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