【问题标题】:C fscanf to read between square bracketsC fscanf 在方括号之间读取
【发布时间】:2015-07-20 22:13:22
【问题描述】:

我有一个包含数据的文件

[姓名] [生日] [身份证]

当我尝试这段代码时

while(fscanf(file,"%s %s %s",name,bdate,uid) == 3)

bdate 获取姓氏] 作为值

如何读取方括号之间的信息。 谢谢。

【问题讨论】:

    标签: c file scanf brackets square-bracket


    【解决方案1】:

    您最好使用fgets() 和真正的解析器,但请尝试使用scanf "scanset" 进行快速修复

    fscanf(file, " [%[^][]] [%[^][]] [%[^][]]", name, bdate, uid)
    //            ^        ^        ^           ordinary whitespace
    //             ^      ^ ^      ^ ^      ^   ordinary characters
    //              ^^---^   ^^---^   ^^---^    scanset specification
    //                ^                         "reverse" scanlist
    //                 ^^                       characters in scanlist
    

    【讨论】:

    • ^]之后是否需要[]
    • “在^] 之后是否需要[]?”是的!请注意,[] 由两个不同的函数组成:[ 是扫描列表(要检查的字符)的一部分,] 终止规范。 scanf 格式字符串中还有一些 '[' 和 ']' 来匹配输入中的文字字符。
    • 我不明白。为什么不fscanf(file, " [%[^]]] [%[^]]] [%[^]]]", name, bdate, uid)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-06-27
    • 2021-05-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多