I am trying to use fscanf() to read in a char that must be preceded and followed by whitespace:
fscanf( input, "%*[ \t]%c%*[ \t]", output )
But unfortunately, the "%*[ \t]" format specifier accepts zero or more matches. Is there anyway I can require it to accept at least one match, or do I need to use something like getc()?
It is possible to solve this post with fscanf() but let us look at a fgetc() approach.
// return 1 on success, else return 0
int GetSpaceCharSpace(FILE *istream, int *ch) {
*ch = fgetc(istream);
if (!isspace(*ch))
return 0;
// consume additional leading spaces as OP said "accept at least one match"
while (isspace(*ch = fgetc(istream)))
;
// Code has a non-white-space
// Success if next char is a white-space
return isspace(fgetc(istream));
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With