鍍金池/ 問答/C  Linux/ 關(guān)于fgets函數(shù)的疑問

關(guān)于fgets函數(shù)的疑問

1.打開一個文檔,文檔的內(nèi)容如下

        012345678901234567890123456789
        012345678901234567890123456789

2.代碼如下

#include <fcntl.h>
#include <stdio.h>
#include <unistd.h>

int main(){
    FILE* fp=NULL;
    if((fp=(fopen("new.dat","r")))==NULL)
        printf("open failure");
    
    char num[10];
    while(1){
        if((fgets(num,10,fp))==NULL){
            if(ferror(fp)){
                printf("read error\n");
                fclose(fp);
                return -1;
            }
            else{
                printf("reach to end\n");
                fclose(fp);
                return 0;
            }
        }        
        else{
            printf("%s\n",num);
        }
        
    }
    fclose(fp);
    return 0;
}

3.結(jié)果如下:

        012345678
        901234567
        890123456
        789
        
        012345678
        901234567
        890123456
        789
        
        
        
        
        
        reach to end

我有些疑問,關(guān)于“789”和“reach to end”之間的那么多的換行是怎么回事?

回答
編輯回答
空白格

很簡單,是因?yàn)槟愕奈臋n內(nèi)容空了許多行

2018年5月16日 13:25