【问题标题】:fscanf with colon (:) delimited datafscanf 用冒号 (:) 分隔数据
【发布时间】:2013-12-08 05:44:43
【问题描述】:

我如何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++;
}

【问题讨论】:

    标签: c file field scanf


    【解决方案1】:

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

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

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

    Demo on ideone.

    【讨论】:

    • 谢谢!这有帮助。像这样的数据文件呢? 039 Mt Albert Rd, Holland landing (lots of spaces) 9.50 043 Queensville Side Road, Queensville (lots of spaces) 10.00
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-04-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-01
    • 2021-01-05
    相关资源
    最近更新 更多