【C】C语言复习_第十天_文件操作

学习内容:

10、文件操作


学习时间:

2020年12月08日19:00-20:30

学习产出:

/*
文件操作
	文件概述
		计算机中的流
			通过不同输入/输出设备之间的数据传输抽象表述为流
			文本流和二进制流
			预定义的流
		文件的概念
		文件的区分
			二进制文件
			文本文件
		文件的缓冲区
		文件指针
			FILE *变量名
		文件位置指针
	文件的打开和关闭
		打开文件
			FILE* fopen(char* filename,char* mode)
			文件的打开方式
		关闭文件
			int fclose(FILE* fp)
	文件的读写
		单字符读写文件
			fputc函数
				int fputc(char ch,FILE* fp)
			fgetc函数
				char fgetc(FILE* fp)
		单行读写文件
			fputs函数
				int fputs(const char *str,FILE* file)
			fgets函数
				char * fgets(char * buf,int maxCount,FILE * file)
		二进制读写文件
			fwrite函数
				unsigned int fwrite(const void*str,unsigned int size,unsigned int count,FILE*file)
			fread函数
				unsigned int fread(const void*str,unsigned int size,unsigned int count,FILE*file)
		格式化读写文件
			fprintf函数
				int fprintf(FILE*file,const char*format,...)
			fscanf函数
				int fscanf(FILE*file,const char*format,...)
		文件检测函数
			feof函数
				int feof(FILE*fp)
			ferror函数
				int ferror(FILE*fp)
			clearerr函数
				void clearerr(FILE*fp)
	文件的随机读写
		文件位置指针的定位
			rewind函数
				void rewind(FILE* fp)
			fseek函数
				int fseek(FILE*fp,long offset,int origin)
			ftell函数
				long fteel(FILE*fp)
		文件随机读写的应用
*/
/*
1、编写程序,在D盘中生成一个文件test.txt,使用fputc()函数文件中写入“hello,welcome to fuzhou”。查看d盘文件,看是否成功。
*/
#include<stdio.h>
#include<stdlib.h>
int main()
{
	FILE*fp;
	fp=fopen("d:\\test.txt","w");
	if(fp==NULL)
	{
		printf("打开文件失败");
		exit(0); 
	}
	fputs("hello,welcome to fuzhou!",fp);
	fclose(fp);
	printf("文件写入成功\n");
	return 0;
}
/*
2、编写程序,在D盘中生成一个文件test2.txt,使用fputs()向文件中写入三行“I am learning C language”“I study hard.. ”“This is a good language.”
*/
#include<stdio.h>
#include<stdlib.h>
int main()
{
	FILE*fp;
	char*str[3];
	int i;
	str[0]="I am learning C language\n";
	str[1]="I study hard..\n";
	str[2]="This is a good language.\n";
	fp=fopen("d:\\test2.txt","w");
	if(fp==NULL)
	{
		printf("打开文件失败");
		exit(0); 
	}
	for(i=0;i<3;i++)
	{
		fputs(str[i],fp);
	}
	fclose(fp);
	printf("文件写入成功\n");
	return 0;
}
/*
3、编写程序,在D盘中生成一个文件test3.txt,使用fwrite()向文件中写入26个英文字母
*/
#include<stdio.h>
#include<stdlib.h>
int main()
{
	FILE*fp;
	char str[26];
	int i,num;
	fp=fopen("d:\\test3.txt","w");
	if(fp==NULL)
	{
		printf("打开文件失败");
		exit(0); 
	}
	for(i=0;i<26;i++)
	{
		str[i]='a'+i;
	}
	num=fwrite(str,sizeof(char)*13,2,fp);
	printf("26个字母已经写入文件test3中\n");
	fclose(fp);
	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值