1.流的读写方式
![在这里插入图片描述](https://img-blog.csdnimg.cn/20210408111443590.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3p4cjkxNg==,size_16,color_FFFFFF,t_70#pic_center)
2.按字符输入
![在这里插入图片描述](https://img-blog.csdnimg.cn/20210408111716595.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3p4cjkxNg==,size_16,color_FFFFFF,t_70#pic_center)
3.举例
3.1 从键盘读取一个字符,并打印到终端上
#include<stdio.h>
int main()
{
char ch;
while(1){
ch=fgetc(stdin);
printf("%c\n",ch);
}
return 0;
}
3.2 读取一个文件,并打印文件中的字符数目
#include<stdio.h>
int main()
{
char ch;
FILE *fd;
int count=0;
fd=fopen("./a.txt","r");
while(1){
ch=fgetc(fd);
if(ch==EOF){
break;
}
printf("%c\n",ch);
count++;
}
printf("数目:%d\n",count);
return 0;
}
3.3 自行输入要查看的文件,并打印文件中的字符数目
#include<stdio.h>
#include<string.h>
int main()
{
char ch;
FILE *fd;
int count=0;
char str[100];
scanf("%s",str);
fd=fopen(str,"r");
while(1){
ch=fgetc(fd);
if(ch==EOF){
break;
}
printf("%c\n",ch);
count++;
}
printf("数目:%d\n",count);
return 0;
}
4.按字符输出
![在这里插入图片描述](https://img-blog.csdnimg.cn/20210408114215880.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3p4cjkxNg==,size_16,color_FFFFFF,t_70#pic_center)
5.举例
5.1 从键盘读入,并打印到终端
#include<stdio.h>
int main()
{
char ch;
while(1){
ch=fgetc(stdin);
fputc(ch,stdout);
}
return 0;
}
5.2 从键盘读入,不覆盖写入a.txt中
#include<stdio.h>
int main()
{
FILE *fd;
char ch;
fd=fopen("./a.txt","a");
for(int i=0;i<3;i++){
scanf("%c",&ch);
fputc(ch,fd);
}
fputs("\n",fd);
return 0;
}
5.3 复制一个文件到另外一个文件中
#include<stdio.h>
int main()
{
char ch;
FILE *pds,*pdm;
pds=fopen("./c.txt","a+");
pdm=fopen("./d.txt","a+");
while(1){
ch=fgetc(pds);
if(ch==EOF){
break;
}
fputc(ch,pdm);
}
fclose(pds);
fclose(pdm);
return 0;
}