gpt4 book ai didi

c++ - fgets - 最大尺寸 (int num)

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


在我的程序中,我正在调用函数 fgets:

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

循环几次,然后处理新的输入(如果有的话)。

在 fgets 规范中,它说:

num: Maximum number of characters to be read (including the final null-character). Usually, the length of the array passed as str is used.

问题是我只想读取指定的数量 - 如果在同一行中,则忽略其余部分。

我发现,fgets 会在下一次调用该函数时读取该行的下一部分。

我怎样才能避免这种行为?

最佳答案

您需要手动执行 - 使用 fgets 处理字符并将结果复制到结果数组,直到找到换行符,转储内容,然后继续使用 fgets

关于c++ - fgets - 最大尺寸 (int num),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6130991/

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