fscanf format specifier usage

I'm trying to find an example of how to use the "width" optional format specifier for fscanf.

According to the page: http://www.cplusplus.com/reference/cstdio/fscanf/

A format specifier for fscanf follows this prototype:
%[*][width][length]specifier

I'm not sure how to actually use this since none of the examples use these options.

My code:

FILE *file;
char buffer[MAX_LEN];
fscanf(file, "%s", buffer);

But what I want to do is specify a limit for %s to prevent buffer overflow.
The [] are being used to denote an optional section.

So if you want a C-string of 20 characters you would have:
width of 20
specifier of s

So your format specifier would be:
%20s
I see ... I guess now the confusion is in how can I get that in when the "MAX_LEN" could be changed? I can't simply put it in this form:

#define MAX_LEN 20

FILE *file;
char buffer[MAX_LEN];
fscanf(file, "%MAX_LENs", buffer);
http://stackoverflow.com/questions/5590381/easiest-way-to-convert-int-to-string-in-c


edit: oh, you're wanting this in C probably. google "itoa".
Last edited on
Topic archived. No new replies allowed.