How to find EOF through fscanf?

后端 未结 3 694
粉色の甜心
粉色の甜心 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: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); 
    }
    

提交回复
热议问题