C fscanf在方括号之间读取

时间:2022-08-28 21:44:04

I have a file which includes datas as

我有一个包含数据的文件

[name surname] [birthday] [id]

[姓氏] [生日] [id]

when i try this code

当我尝试这个代码

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

bdate gets surname] as a value

bdate得到姓氏]作为一个值

how can i read informations between square brackets. thanks.

我怎样才能阅读方括号之间的信息。谢谢。

1 个解决方案

#1


You're better off with fgets() and a real parser, but try the using scanf "scanset" for a quick fix

你最好使用fgets()和一个真正的解析器,但尝试使用scanf“scanset”进行快速修复

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

#1


You're better off with fgets() and a real parser, but try the using scanf "scanset" for a quick fix

你最好使用fgets()和一个真正的解析器,但尝试使用scanf“scanset”进行快速修复

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