c - How should i limit the number of characters interpretted by fscanf? -
i'm writing program read stdin
, writes reads stdout
, unescaping escaped hex numbers finds. numbers want read 8 bit. have far
while((c = fgetc(stdin)) != eof) { if(c == '%') { fscanf(stdin,"%x",&r); printf("%i \n",r); } }
this works fine, except fact when write %fff
standard input reads 3 digit hex number. how should limit fscanf
reading 2 characters? have thought reading next 2 characters buffer , sscanf'ing
that, feels rather inelegant me.
if want scanf
(et al) read 2 characters, tell so:
scanf("%2x", &r);
see e.g. this reference information scanf
formatting.
Comments
Post a Comment