How to ignore spaces in fscanf ()

I need to use fscanfto ignore all spaces and not keep it. I tried to use something like a combination between (*)and [^\n]how: fscanf(file," %*[^\n]s",); Of course it crashed, is there any way to do this only with fscanf?

code:

int funct(char* name)
{
   FILE* file = OpenFileToRead(name);
   int count=0; 
   while(!feof(file)) 
   {
       fscanf(file," %[^\n]s");
       count++;
   }
   fclose(file);
   return count;
}

Solved! change the original fscanf()to   fscanf(file," %*[^\n]s"):; read the entire line exactly how fgets(), but did not save it!

+4
source share
3 answers

Your code fails, because you have %sin your format specifier in fscanfthe call, and you do not pass fscanfand char *to which you want to write the found string.

. http://www.cs.utah.edu/~zachary/isp/tutorials/io/io.html.

+3

("") fscanf , , . , , :

fscanf(file, " "); // skip whitespace
getc(file);        // get the non-whitespace character
fscanf(file, " "); // skip whitespace
getc(file);        // get the non-whitespace character

fscanf(file, " %c %c", &char1, &char2); // read 2 non-whitespace characters, skipping any whitespace before each

fscanf fgets?

+2

on the fscanf man page:

   A directive is one of the following:
  ·      A sequence of white-space characters (space, tab, newline, etc.;
          see isspace(3)).  This directive matches  any  amount  of  white
          space, including none, in the input.

So

fscanf(file, " %s\n");

will skip all spaces before reading in characters.

+1
source

All Articles