1.16文件其它函数讲解及文件收尾
如何使用fputc;如何使用feop,fgetc
代码展示
如何使用fputc
**#include<stdio.h>
#include<string.h>
int main()
{
FILE *fp;
char *str = "chenlichen hen shuai";
int i;
int len = strlen(str);
fp = fopen("./test.txt","w+");
for(i=0;i<len;i++){
fputc(*str,fp);
str++;
}
// int fputc(int c, FILE *stream);
fclose(fp);
return 0;
}**
代码展示
如何使用feop,fgetc
#include<stdio.h>
#include<string.h>
int main()
{
FILE *fp;
int i;
char c;
fp = fopen("./test.txt","r");
while(!feof(fp)){// nonezero if reach end of file
c = fgetc(fp);
printf("%c",c);
}
// int fputc(int c, FILE *stream);
fclose(fp);
return 0;
}

本文介绍了C语言中用于文件操作的fputc和fgetc函数。fputc函数用于向文件写入单个字符,而fgetc函数则用于从文件读取单个字符。通过示例代码展示了如何使用这两个函数分别将字符串写入文件和从文件读取内容。

被折叠的 条评论
为什么被折叠?



