当前位置: 首页 > 工具软件 > FE > 使用案例 >

文件:feof的用法

谢泉
2023-12-01

当文件没读到尾巴之前,feof(fd)=0  ;

当文件读到尾巴 ,feof(fd) =  !0   ;

#include<stdio.h>
#include<string.h>
int main()
{
        FILE *fd;
        char *writebuff = "huangcaichenghenshuaiqwertyuioasdfghjklzxcvbnnnnnnm123456789";
        char readbuff[128] = {0};
        fd = fopen("./text2","w+");
        printf("writebuff=%s\n",writebuff);
        int n_fwrite = fwrite(writebuff,sizeof(char),strlen(writebuff),fd);

        int size = fseek(fd,0,SEEK_SET);
        //feof(fd)=0  before fd to zero; feof(fd)=nonzero when fd to zero 
        while(!feof(fd)){
        int n_fread = fread(readbuff,sizeof(char),4,fd);
        printf("read:%d bytes;context:%s\n",n_fread,readbuff);
        }
        fclose(fd);
        return 0;

}
hcc@ubuntu:~/LinuxSystemProgramming/FILE$ ./a.out
writebuff=huangcaichenghenshuaiqwertyuioasdfghjklzxcvbnnnnnnm123456789
read:4 bytes;context:huan
read:4 bytes;context:gcai
read:4 bytes;context:chen
read:4 bytes;context:ghen
read:4 bytes;context:shua
read:4 bytes;context:iqwe
read:4 bytes;context:rtyu
read:4 bytes;context:ioas
read:4 bytes;context:dfgh
read:4 bytes;context:jklz
read:4 bytes;context:xcvb
read:4 bytes;context:nnnn
read:4 bytes;context:nnm1
read:4 bytes;context:2345
read:4 bytes;context:6789
read:0 bytes;context:6789

 

 类似资料: