Search code examples
cfilescanfbracketssquare-bracket

C fscanf to read between square brackets


I have a file which includes datas as

[name surname] [birthday] [id]

when i try this code

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

bdate gets surname] as a value

how can i read informations between square brackets. thanks.


Solution

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

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