2

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.

Cœur
  • 37,241
  • 25
  • 195
  • 267
umtkas
  • 62
  • 9

1 Answers1

2

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
pmg
  • 106,608
  • 13
  • 126
  • 198
  • Are `[]` after `^]` required? – Spikatrix May 11 '15 at 15:00
  • "Are `[]` after `^]` required?" YES! Note that the `[]` is composed of two distinct funcions: the `[` is part of the scanlist (characters which are to be checked) and the `]` terminates the specification. Also there are a few more '[' and ']' in the `scanf` format string to match literal characters in input. – pmg May 11 '15 at 15:14
  • I did not understand. Why not `fscanf(file, " [%[^]]] [%[^]]] [%[^]]]", name, bdate, uid)`? – Spikatrix May 12 '15 at 06:55