Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to ignore whitespaces in fscanf()

Tags:

c

scanf

I need to use fscanf to ignore all the white spaces and to not keep it. I tried to use something like the combination between (*) and [^\n] as: fscanf(file," %*[^\n]s",); Of course it crashed, is there any way to do it 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 all the line exactly as fgets() but didnt keep it!

like image 937
olsynt Avatar asked Dec 20 '13 21:12

olsynt


People also ask

Does fscanf ignore whitespace?

A white space character causes fscanf(), scanf(), and sscanf() to read, but not to store, all consecutive white space characters in the input up to the next character that is not white space.

How do I make scanf ignore Whitespaces?

The secret to getting scanf to perform this way is to put a blank in the format string before the %c format specifier. The blank tells scanf to skip white space and it will actually skip any number of white space characters before reading and storing a character.

Why does scanf stop white space?

scanf() just stops once it encounters a whitespace as it considers this variable "done".

Does scanf work with spaces?

Adding a whitespace character in a scanf() function causes it to read elements and ignore all the whitespaces as much as it can and search for a non-whitespace character to proceed. scanf("%d "); scanf(" %d"); scanf("%d\n"); This is different from scanf("%d"); function.


3 Answers

Using a space (" ") in the fscanf format causes it to read and discard whitespace on the input until it finds a non-whitespace character, leaving that non-whitespace character on the input as the next character to be read. So you can do things like:

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

or

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

from:

Ignoring whitepace with fscanf or fgets?

like image 116
gaurav5430 Avatar answered Oct 06 '22 09:10

gaurav5430


Your code is crashing because you have a %s in your format specifier in the fscanf call, and you don't pass fscanf a char * to which you want it to write the string it finds.

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

like image 45
Andrey Mishchenko Avatar answered Oct 06 '22 08:10

Andrey Mishchenko


from 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 whitespace before reading in characters.

like image 3
Chris J. Kiick Avatar answered Oct 06 '22 09:10

Chris J. Kiick