栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

C语言文件——字符读出与写入

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

C语言文件——字符读出与写入

C语言文件——字符读出与写入

conclusion:

  • the steps of read file
步骤
1.定义文件源
2.fopen打开文件
3.fgetc读取文件(按照字符读取)
4.当读取的内容不是EOF文件尾的时候,重复步骤 3
5.关闭文件
  • the code to validate above contents
//字符读或写入文件
#include
#include


int main() {
	//读出文件的内容并显示到屏幕
	
	char ch;
	char *source = "1.txt";
	char *op = "r+t";
	FILE *fp;
	fp = fopen(source, op);
	if (fp == NULL) {
		puts("文件不存在!打开失败");
	}
	while ((ch = fgetc(fp)) != EOF) {
		putchar(ch);
	}
	if (ferror(fp)) {
		printf("文件读取错误!");
	} else printf("读取成功!");
	fclose(fp);
	return 0;
}
  • the steps of input file
步骤
1.定义文件源
2.fopen打开文件
3.fputc写入字符到文件(按照字符写入)
4.当读取的内容不是EOF文件尾的时候,重复步骤 3
5.关闭文件
  • the code to validate above contents
//字符读或写入文件
#include
#include


int main() {
	//以字符的方式将内容写入到指定的文件
	

	char ch;
	char *source = "1.txt";
	char *op = "r+t";
	FILE *fp;
	char *source2 = "2.txt";
	char *op2 = "a+t";
	fp = fopen(source2, op2);
	if (fp == NULL) printf("文件打开失败!");
	printf("请输入一行字符:");
	while ((ch = getchar()) != 'n') {
		fputc(ch, fp);
	}
	if (ferror(fp)) printf("文件写入失败!");
	else printf("文件写入成功!");
	fclose(fp);
	return 0;
}
  • put on stream! 考研人!!!
    2022/5/29
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/934425.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号