如何通过fscanf找到EOF? [英] How to find EOF through fscanf?

查看:81
本文介绍了如何通过fscanf找到EOF?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 fscanf() 的帮助下通过文件读取矩阵.我怎样才能找到EOF?即使我尝试在 arr[] 中捕获的每个字符串之后找到 EOF,我也无法找到它.

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("\ni caught it\n");//here we have to exit.
    }
return 0;
}

我想用 EOF 退出循环而不是 count .如何解决?

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

推荐答案

fscanf - "成功时,函数返回成功读取的项目数.这个计数可以匹配预期的读取数或在匹配失败的情况下更少 - 甚至为零.如果在成功读取任何数据之前输入失败,则返回 EOF."

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."

因此,您可以检查它是否是 == EOF,而不是像现在这样对返回值不做任何处理.

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

调用 fscanf 时应该检查 EOF,而不是检查 EOF 的数组槽.

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

这篇关于如何通过fscanf找到EOF?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆