1

I am trying to print out a file using this code but I keep getting the error that asks me to use strtol instead of fscanf on clion.

#include <stdlib.h>
#include <stdio.h>
#define SSIZE 20

int main(void) {
        FILE *fin;
        int   i;
        float f;
        char  string[SSIZE];
          
          /* maximum SIZE of string, including NUL */
          /* define a file pointer */
          /* define variables to store data */
        
        fin = fopen("data.txt", "r");
        fscanf(fin, "%d %f %s", &i, &f, string);
          
          /* open data.txt file for read */
          /* read file and save data to variables */
        
        printf("%d %f %s\n", i, f, string);
          
          /* print values of variables to screen */
        
        fclose(fin); 
            
          /* close file properly */
        return 0;
}
uben
  • 1,221
  • 2
  • 11
  • 20
PAMAF
  • 11
  • 2
  • Consider using `strtol()` as told or use another compiler. – MikeCAT Mar 30 '21 at 10:42
  • how would I use strtol in this instance? – PAMAF Mar 30 '21 at 10:43
  • It's strange that this is an error instead of a warning. Do you have `-Werror` enabled in your compiler options? i don't use CLion so I don't know if it's set by default. – mediocrevegetable1 Mar 30 '21 at 10:47
  • 3
    Or just check that fscanf returns 3. – stark Mar 30 '21 at 10:48
  • My bad, it is a warning. But still, it does not read my file. – PAMAF Mar 30 '21 at 10:59
  • I tried your code, and it worked for me. What compiler are you using ? What is the content of the file ? While your code doesn't read the file, try to simplify it, until you catch the problem. – uben Mar 30 '21 at 11:16
  • For the warning, maybe, you can pass `&string[0]` even if it is equivalent, your compiler might be confused by the fact it is an array instead of a pointer. – uben Mar 30 '21 at 11:20
  • I am using Clion as a compiler – PAMAF Mar 30 '21 at 11:42
  • You should always check return value of all IO functions. Did you successfully open the file? How many values were converted by `fscanf` etc. – Gerhardh Mar 30 '21 at 12:04
  • @uben why would the compiler be confused? It just warns that `fscanf` should be replaced by a better function for the reason that is also specified in the warning. – Gerhardh Mar 30 '21 at 12:06
  • Oh I see, I though the compiler was complaining about the %s, not the %d and %f. So, the warning of the compiler is fair. – uben Mar 30 '21 at 12:47
  • 2
    You should *ALWAYS* check the return value of [fscanf()!](https://man7.org/linux/man-pages/man3/fscanf.3p.html) As well as checking for "null" return from fopen()! – paulsm4 Mar 31 '21 at 05:29
  • You cannot use ANY input function correctly unless you ***check the return*** to determine whether the input succeeded or failed. With the `scanf()` family, you cannot use the `"%s"` conversion specifier correctly unless you provide a *filed-width* modifier to limit the number of characters read to the size of your storage (-1 saving room for the nul-terminating character), e..g. `if (scanf(fin, "%d %f %19s", &i, &f, string) != 3) { /* handle the error */ }`. Without the field-width modifier, `"%s"` is no safer than `gets()`. – David C. Rankin Mar 31 '21 at 06:44
  • See [Why gets() is so dangerous it should never be used!](https://stackoverflow.com/q/1694036/3422102) – David C. Rankin Mar 31 '21 at 06:44

0 Answers0