fgetc的用法(一个字符的显示文件中的数据)

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

int main(int argc, char *argv[])
{
FILE
*fp;
char ch;
fp
=fopen("test","r");//fopen产生一个文件指针
while((ch=fgetc(fp))!=EOF) //以文件流方式读取文件,以EOF结尾
{
sleep(
1);
putc(ch,stdout);
fflush(stdout);
//刷新缓冲区,让输出显示
}
fclose(fp);
return 0;
}


程序2:

#include <stdio.h>
#include
<stdlib.h>
#include
<unistd.h>
#include
<fcntl.h>
int main(int argc, char *argv[])
{
char ch[10];
int fd;
fd
= open("./test",O_RDONLY);
while(read(fd,ch,1) != 0)
{
sleep(
1);
write(
1,ch,1);
// fflush(stdout);
}
close(fd);
return 0;
}

原文地址:https://www.cnblogs.com/hnrainll/p/2055993.html