【发布时间】:2015-07-20 22:13:22
【问题描述】:
我有一个包含数据的文件
[姓名] [生日] [身份证]
当我尝试这段代码时
while(fscanf(file,"%s %s %s",name,bdate,uid) == 3)
bdate 获取姓氏] 作为值
如何读取方括号之间的信息。 谢谢。
【问题讨论】:
标签: c file scanf brackets square-bracket
我有一个包含数据的文件
[姓名] [生日] [身份证]
当我尝试这段代码时
while(fscanf(file,"%s %s %s",name,bdate,uid) == 3)
bdate 获取姓氏] 作为值
如何读取方括号之间的信息。 谢谢。
【问题讨论】:
标签: c file scanf brackets square-bracket
您最好使用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)?