#include<stdio.h>
#include<stdlib.h>
int write()
{
FILE *fp = NULL;
fp = fopen("C:\\Users\\ASUS\\Documents\\HBuilderProjects\\op.txt","w");//写,提前清空
if(fp == NULL)
{
perror("fopen");
return -1;
}
//fputc('a',stdout);
char ch = 'a';
while(ch <= 'z')
{
fputc(ch,fp);
ch++;
}
fclose(fp);
fp=NULL;
}
int read()
{
FILE *fp = NULL;
fp = fopen("C:\\Users\\ASUS\\Documents\\HBuilderProjects\\op.txt","r");//只读不可以清空
if(fp == NULL)
{
perror("fopen");
return -1;
}
char ch;
while(1)
{
ch = fgetc(fp);
printf("ch = %c\n",ch);
//if(ch == EOF)
if(ch == -1)
{
break;
}
}
fclose(fp);
fp=NULL;
}
int main()
{
write();
read();
}
C语言————fgetc和fputc的使用
最新推荐文章于 2021-06-04 18:48:02 发布