gpt4 book ai didi

c - 这个C程序有什么问题,文件读取?

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

我正在编写一个 C 程序,其中我必须将一个大文件读入 block (每个 block 16 个字符)。为此,我编写了以下代码。

while(fgets(temp2,16,fp)!=NULL)
{
printf("\n%d:%s, len = %d", cnt++, temp2, strlen(temp2));
}

但它只读了 15 个字符。这有什么问题?我该怎么办?

最佳答案

What is wrong with this?

没有。它正在做你告诉它的事情。它将读取最多 15 个字符,除非遇到换行符或文件结尾,然后在最后一个写入的字节之后以 null 终止该字节。

what should i do?

阅读reference for fgets ,其中指出:

char *fgets( char *str, int count, FILE *stream );

Reads at most count - 1 characters from the given file stream and stores them in str. The produced character string is always null-terminated. Parsing stops if end-of-file occurs or a newline character is found, in which case str will contain that newline character.

您可能想使用的是 fread,而不是 fgets,因为您的问题是关于读取字符 block 而不是换行分隔的字符串。

关于c - 这个C程序有什么问题,文件读取?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35424913/

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