C Reference ยท stdio.h
C sscanf() and fscanf() functions
Reference for C sscanf() and fscanf(): parse values from a string or a FILE stream, formats, the %n trick, return values and safe line-by-line parsing.
sscanf() reads values out of a string you already have in memory, and fscanf() reads them from any FILE * stream. Both use the same format language as scanf(); only the source differs. scanf(fmt, ...) is the same as fscanf(stdin, fmt, ...).
The best-known pattern for robust input is to read a whole line with fgets() and then parse it with sscanf(). If the parse fails you still have the original line to report, and the stream stays in a clean state, which solves most of the scanf pitfalls.
#include <stdio.h> int sscanf(const char *restrict s, const char *restrict format, ...); int fscanf(FILE *restrict stream, const char *restrict format, ...);
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.
Syntax
int sscanf(const char *restrict s, const char *restrict format, ...); int fscanf(FILE *restrict stream, const char *restrict format, ...);
| Name | Description |
|---|---|
s |
The null-terminated string to read from. sscanf treats the end of the string like end-of-file. |
stream |
An open, readable FILE *. |
format, ... |
Format and result pointers, exactly as for scanf. |
Returns: the number of items assigned, or EOF if the input ended (or a read error happened) before the first conversion.
Errors / undefined behavior: wrong pointer types, missing arguments and unbounded %s into a small array are undefined behavior. sscanf on a string that is not terminated is undefined as well. Source and destination of a sscanf must not overlap.
Usage notes
- Return value.
3means three fields were stored;0means the first conversion failed (a mismatch);EOFmeans no input at all (an empty string forsscanf). Compare with the exact expected count. sscanfdoes not remember its position. Each call starts at the beginning of the string. To parse a sequence, use%nto learn how many characters were consumed and advance your pointer:p += used.- Detecting trailing junk.
sscanf("12abc", "%d", &n)returns 1. To demand that the entire string was a number, add%nafter the conversion and testtext[used] == '\0', or usestrtol(), which gives better error information. - Overflow is undefined.
sscanf("99999999999", "%d", &n)has undefined behavior;strtolreportsERANGEinstead. - Widths protect buffers. Always bound
%sand%[:%15sforchar[16]. %[^,]reads up to a comma, which is how you split simple comma-separated fields; an empty field makes the match fail (a%[conversion needs at least one character).fscanfloops end on mismatch too. Awhile (fscanf(...) == 2)loop stops at a bad row, not only at end of file, so checkfeof(f)afterwards if the difference matters (seefeofandferror).- Line oriented files are better handled with
fgetsplussscanf, sincefscanflets%scross line boundaries (newlines are whitespace). - Speed.
sscanfis convenient, not fast; it parses the format string on every call. For large inputs, write a tokenizer or usestrtol/strtod.