gpt4 book ai didi

c - fscanf 返回值

转载 作者:太空狗 更新时间:2023-10-29 17:00:01 24 4
gpt4 key购买 nike

fscanf 读取文件中的数据时返回什么。例如,

int number1, number2, number3, number4, c;

c = fscanf (spFile, "%d", &number1);
//c will be 1 in this case.

c = fscanf (spFile, "%d %d %d %d", &number1, &number1, &number3, &number4);
//in this case, c will return 4.

我只想知道为什么它会根据参数的数量返回这样的值。

最佳答案

来自manpage for the Xscanf family功能:

Upon successful completion, these functions shall return the number of successfully matched and assigned input items; this number can be zero in the event of an early matching failure. If the input ends before the first matching failure or conversion, EOF shall be returned. If a read error occurs, the error indicator for the stream is set, EOF shall be returned, and errno shall be set to indicate the error

所以您对 fscanf 的第一次调用返回 1,因为一个输入项 (&number1) 已成功与格式说明符 %d 匹配。您对 fscanf 的第二次调用返回 4,因为所有 4 个参数都匹配。

关于c - fscanf 返回值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15318360/

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