gpt4 book ai didi

c - 带冒号的 fscanf ( :) delimited data

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

我如何fscanf这一段数据?数据和分隔符之间没有空行是 ':'

VS1234567890654327:Rob Fordfirst:001:200
VS1234567890654312:Steven Harper:200:010

我的代码

while(3==fscanf(filename, "????", &string[size], &name[size], &number1[size], &number2[size])) {
//printf("%s - %s - %.3d - %.3d", string[size], name[size], number1[size], number2[size]));
size++;
}

最佳答案

您可以将分隔符作为格式的一部分包含到 fscanf 中,如下所示:

while (4 == fscanf(filename, "%[^:]:%[^:]:%d:%d", string[size], name[size], &number1[size], &number2[size])) {
...
}

注意 %[^:] 格式说明符的使用。它说“接受除 ':' 以外的任何字符”。另请注意,char* 参数在传递时没有与号,因为它们已经是指针。

Demo on ideone.

关于c - 带冒号的 fscanf ( :) delimited data,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20450333/

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