Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
764 views
in Technique[技术] by (71.8m points)

c - How to find EOF through fscanf?

I am reading matrix through file with the help of fscanf(). How can i find EOF? Even if i try to find EOF after every string caught in arr[] then also i am not able to find it.

with the help of count i am reading the input file

-12 23 3

1 2 4

int main()
{
    char arr[10],len;
    int count=0;

    FILE *input= fopen("input.txt", "r");

    while(count!=7)
    {   
           fscanf(input,"%s",arr);
          //storing the value of arr in some array.
                   len=strlen(arr);
           count++;

           if(arr[len+1]==EOF)
           printf("
i caught it
");//here we have to exit.
    }
return 0;
}

Instead of count i want to exit through the loop with the EOF . how can it be solved?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

fscanf - "On success, the function returns the number of items successfully read. This count can match the expected number of readings or be less -even zero- in the case of a matching failure. In the case of an input failure before any data could be successfully read, EOF is returned."

So, instead of doing nothing with the return value like you are right now, you can check to see if it is == EOF.

You should check for EOF when you call fscanf, not check the array slot for EOF.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...