This question already has an answer here:
I'm developing a triangle calculation and trying to tweak my sscanf
to ignore spaces, newlines \n
and tabs \t
. How can I do that?
I have:
if(sscanf(str, "{ [ %lf ; %lf ] , [ %lf ; %lf ] , [ %lf ; %lf ] }", &x_1, &y_1, &x_2, &y_2, &x_3, &y_3) == 6)
which perfectly works for inputs like these:
1) {[0;0],[19;10],[0;10]}
2) { [ 0 ; 0], [12;0],[0 ;10] }
but it doesn't work for something like this
1) {
[
0
;
15
]
, [ 112 ; 0 ] ,[112;15]}
What do I need to fix?
Note that
sscanf()
largely ignores white space by default. A white space in the format string matches zero or more white space characters — blanks, tabs, newlines. All but three of the conversion specifications ignore leading white space anyway — the three exceptions being%c
,%[…]
(scan sets) and%n
— so with all the white space in your format string, neither blanks nor tabs should affect the conversion, unless there is white space before the first{
(which you could manage by adding a space in the format string before the{
). The problem, therefore, is probably not in thesscanf()
but in the characters in the data.If you are reading the data with
fgets()
, you'll need to accumulate the multiple lines into a single string buffer. If you replacedsscanf()
with eitherscanf()
orfscanf()
, then your format string would read the newlines as needed (but both would leave any newline after the close brace}
to be read by future input operations). Whensscanf()
processes the data, the string must contain all the requisite characters, nominally up to the end}
though up to the last digit of the sixth number would be sufficient.Given this code — where the third string represents what you say is in your third multiline data (I'm assuming that the leading
1)
and2)
plus space are noise in the question since the format string does not attempt to parse those):I get the output:
As you can see, all three scan operations are successful. That suggests that what you think you've got as the third (multiline) string isn't what you've actually got.
I recommend doing a byte-by-byte dump of the data that fails, to see where the problem is. Also notice that I capture and print the return value from
sscanf()
; that will help you determine where the faulty character is. And note thatsscanf()
has no way to tell you that it failed to match the final]
or}
in the format string — you'll never know whether those were matched OK with the current format.As said in the comments,
fgets
doesn't get you the whole thing but only a line at a time. As an alternative to concatenating multiplefgets
reads, how about processing your input char by char and using}
as termination:Test
Input file
Output