How to find EOF through fscanf?

后端 未结 3 685
粉色の甜心
粉色の甜心 2020-12-09 16:30

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

相关标签:
3条回答
  • 2020-12-09 17:04
    while (fscanf(input,"%s",arr) != EOF && count!=7) {
      len=strlen(arr); 
      count++; 
    }
    
    0 讨论(0)
  • 2020-12-09 17:12

    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.

    0 讨论(0)
  • 2020-12-09 17:22

    If you have integers in your file fscanf returns 1 until integer occurs. For example:

    FILE *in = fopen("./task.in", "r");
    int length = 0;
    int counter;
    int sequence;
    
    for ( int i = 0; i < 10; i++ ) {
        counter = fscanf(in, "%d", &sequence);
        if ( counter == 1 ) {
            length += 1;
        }
    }
    

    To find out the end of the file with symbols you can use EOF. For example:

    char symbol;
    FILE *in = fopen("./task.in", "r");
    
    for ( ; fscanf(in, "%c", &symbol) != EOF; ) {
        printf("%c", symbol); 
    }
    
    0 讨论(0)
提交回复
热议问题