What does equals to 2 means in this line fscanf (fi,"%d %d", &used,&quota)==2?

I started to learn files and I don't exactly understand how they are read.

  1. My first question is: What does equals to 2 means in this line?

    fscanf(fi,"%d %d", &used,&quota) == 2
    

    Shouldn't it be equal to EOF, how will it understand that the program has reached the end?

  2. How does the file with data in one line differs from the file with data in several lines. How does it read \n and how can I read only the first line or second for example?

2 answers

  • answered 2019-09-21 18:06 Marco Bonelli

    Take a look at the manual page for fscanf:

    RETURN VALUE

    Upon successful completion, these functions shall return the number of successfully matched and assigned input items; this number can be zero in the event of an early matching failure.

    So in this case, the check fscanf(fi,"%d %d", &used,&quota) == 2 is to make sure that both used and quota were correctly read from the file.


    Shouldn't it be equal to EOF, how will it understand that the program has reached the end?

    Well yes, only in that case though. Again, from the manual page:

    If the input ends before the first conversion (if any) has completed, and without a matching failure having occurred, EOF shall be returned.


    How does the file with data in one line differs from the file with data in several lines. How does it read \n and how can I read only the first line or second for example?

    When you put a space (like here "%d %d") in the format string, fscanf will skipp all whitespace it encounters (including spaces, \n, and other characters). This means that if you input 1 2 or 1 2 or 1\n2 all three inputs will be read with no problem.

  • answered 2019-09-21 18:51 Steve Summit

    How does the file with data in one line differs from the file with data in several lines. How does it read \n and how can I read only the first line or second for example?

    This is another fun fact about the scanf family. Given the call

    fscanf(fi, "%d %d", &used, &quota)
    

    and the input line

    10 20
    

    it will successfully read 10 for used and 20 for quota, which is probably what you had in mind. But if you gave it the input

    10
    20
    

    it would also perfectly happily read 10 for used and 20 for quota. In fact it would work just as well for the input

    10
    
    20
    

    with a blank line in there. Furthermore, there's absolutely no way for you (that is, the author of the code that calls fscanf) to determine whether the input was on one line, or two lines, or what.

    Shouldn't it be equal to EOF, how will it understand that the program has reached the end?

    It's true: if fscanf hits end of file before converting any input, it returns EOF, and that's something you might want to know about. But that's not the only error condition you might want to know about.

    Suppose the input line says

    10 abc 20
    

    In this case, the fscanf call will return 1, indicating that it was able to read the value 10 for used, but it wasn't able to read anything for quota.

    The reason to check for 2 (as in the code you asked about ) is that only if fscanf returns 2 did it read and convert a value for both variables.