Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to force scanf to match whitespace?

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()?

like image 558
Bennett Lynch Avatar asked Dec 06 '25 03:12

Bennett Lynch


1 Answers

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));
}
like image 59
chux - Reinstate Monica Avatar answered Dec 08 '25 18:12

chux - Reinstate Monica