2

I am getting the following compiler warning when I use the following to skip a line when reading a file with fscanf with C:

warning: too many arguments for format [-Wformat-extra-args]
fscanf(myFile, "%*[^\n]\n", NULL);  

The program works perfectly fine. However, I wonder if there is an approach to skip a line in a file that is as minimalistic as the above and does not give compiler warnings (or a simple edit to the above would be ideal)? This approach to skipping a line was taken from How to skip the first line when fscanning a .txt file? where there is no mention of any such warning. Other methods of skipping a line are presented in this previous question; however, none are as minimalistic as the above.

Community
  • 1
  • 1
Anakin
  • 33
  • 1
  • 7
  • 1
    `fscanf(myFile, "%*[^\n]\n", NULL);` --> `fscanf(myFile, "%*[^\n]\n");` but other code would be better. – chux - Reinstate Monica Jan 09 '17 at 02:04
  • Simple. To the point. No more compiler warning. Cheers, @chux. – Anakin Jan 09 '17 at 02:06
  • 1
    You should be aware that although the `fscanf()` call will only return 0 (or EOF if there is no more input when it starts scanning), it will only read a newline if there is at least one non-newline character before the next newline. If you want line based input, don't use the `scanf()` family of functions; use [`fgets()`](http://pubs.opengroup.org/onlinepubs/9699919799/functions/fgets.html) (or, on POSIX machines, use [`getline()`](http://pubs.opengroup.org/onlinepubs/9699919799/functions/getline.html)). You can then use `sscanf()` to parse the line if appropriate. – Jonathan Leffler Jan 09 '17 at 02:59
  • Interesting. Thanks, Jonathan! I will look into fgets() as both you and chux suggest that this implementation of fscanf may not be the best choice. – Anakin Jan 09 '17 at 03:03

1 Answers1

3

Remove the NULL. The warning is because the compiler understood from the * that you dont want to store the result anywhere (but just advance the file with the specified pattern). But then it gets somehow "surprised" that you specify a location (be it NULL).

A.S.H
  • 29,101
  • 5
  • 23
  • 50