gpt4 book ai didi

C - sscanf() %d 拾取零并导致目标 &curritem.stock 为 NULL

转载 作者:行者123 更新时间:2023-11-30 20:55:39 25 4
gpt4 key购买 nike

关闭。这个问题需要debugging details .它目前不接受答案。












想改进这个问题?将问题更新为 on-topic对于堆栈溢出。

6年前关闭。




Improve this question




sscanf() 非常适合我面临的问题,但它正在做一些意想不到的事情。

tmp 是从文件中读取的一行,我需要将部分行解析为单独的数据。 curritem 是一个具有 int Dollar、int cent、cat[]、int stock 和 int history[] 的结构。

我遇到的奇怪问题是,当 %d 拾取双零 (00) 时,它会适本地将整数零插入目标,但在遇到单零 (0) 时,整数不会适本地添加到目标;例如,在收集与 history[] 相关的数据时,一切都很好,将 1、4、8、2、13 等添加到数组中,但是一个零会切断那里的所有内容,就好像 NULL 在那一点终止一样,我认为这是相关的。

代码:

sscanf(tmp, "%d.%d%s %d %d %d", &curritem.dollar, &curritem.cent, 
curritem.cat, &curritem.stock, &curritem.history[0],
&curritem.history[1]);

我省略了 curritem.history[] 的一些索引,因为它使必要的信息变得困惑。

如果正确添加了所有整数,我应该得到的示例:
curritem.history[0...11] = 5 2 6 1 0 11 9 0 15 0 7 10

目前的实际结果是:
curritem.history[0...11] = 5 2 6 1

当 sscanf() 通过 %d 看到单个 '0' 并将其添加到 &struct.intarray[n] 时,某些东西会死掉

有任何想法吗?

尽量不要为我的一般程序功能推荐一个完全不同的解决方案,sscanf() 在其他各个方面都创造了奇迹。

感谢您的时间!

编辑:

精确输入的片段:

WL162343抓绒衬里双L牛仔裤49.95裤子3 8 1 0 1 0 7 1 5 2 10 2 6

所有信息都成功获取,直到类别之后 - 在本例中为“裤子”。

在这个例子中,我的名为 history 的 int 数组应该保存值
3  8  1  0  1  0  7  1  5  2 10  2  6

但是在打印数组中的每个项目时:
int n = 0;
for (n = 0; curritem.history[n]; n++) {
printf("%i ", curritem.history[n]);
}

以下输出结果:
3  8  1

编辑:

用于打印的“for”循环无法区分整数“0”和数组的空终止符。恼人的。

最佳答案

你怎么看curritem.history[n]将返回 for包含零时的条件?

关于C - sscanf() %d 拾取零并导致目标 &curritem.stock 为 NULL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31712160/

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