O'Reilly logo

Just Enough C/C++ Programming by Guy W. Lecky-Thompson

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Using sscanf

The companion to scanf, sscanf is used to read data from a string (perhaps acquired from a file) into the variables, rather than from the keyboard. As with sprintf, the first parameter is a variable, but it provides the source of the formatted string, rather than the target.

sscanf (<source string>, <format>, <target variables. . .> )

So the sscanf function will break up the source string according to the format specifiers included in the format string and place the results in the target variables. The same conversion rules apply with sscanf; in other words, although it can be used in conjunction with field width specifiers, they will not discard data in the way you might expect.

A simple usage of the sscanf function might be as ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required