C语言文件操作(简洁且生动版)—代码解析

该文详细介绍了C语言中的文件操作,包括使用fopen打开文件,fscanf和fprintf进行读写,fgetc和fputc处理单个字符,fgets和fputs处理字符串,以及二进制文件的fwrite操作和fseek的文件定位功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1:文件操作:

fopen——打开文件后可以做的操作(前提—打开文件)

1:向文件中写入数据 。

2:从文件中读取数据。

fclose——关闭文件。

2:文件的打开方式 

 3.文件的顺序读写

各个输入输出函数的具体案例:

fscanf(); fprintf();以及stdin,stdout,stderr的应用。

#include <stdio.h>
#include <stdlib.h>
int main() {
	FILE* fpr = NULL;
	FILE* fpw = NULL;
	fpr = fopen("a.txt","r");//从a.txt中读取数据,并将读取得到的数据写入到b.txt中
	fpw = fopen("b.txt","w");
	if (fpr == NULL) {
		printf("打开失败!\n");
	}
	if (fpw == NULL) {
		printf("打开失败!\n");
	}
	int i=0;
	char name[20] = {0};
	double d = 0;
	fscanf(fpr,"%d%s%lg",&i,name,&d);//与scanf();函数类似——读取
	fprintf(fpw,"%d %s %lg",i,name,d);//与printf()函数也类似——写入
	printf("i=%d,name=%s,d=%.1lf\n",i,name,d);
	//从标准输入设备键盘上获取数据保存到i,name,d中
	fscanf(stdin,"%d%s%lg",&i,name,&d);
	//将i,name,d打印输出到标准输出设备显示器上
	fprintf(stdout,"%d %s %lg\n",i,name,d);
	//将i,name,d打印输出到标准输出设备显示器上
	fprintf(stderr,"%d %s %lg\n", i, name, d);
	fclose(fpr);
	fclose(fpw);
	fpr = NULL;
	fpw = NULL;
	return 0;
}

fgetc();fputc();

#include <stdio.h>
#include <stdlib.h>
int main() {
	FILE* fpr = NULL;
	FILE* fpw = NULL;
	fpr = fopen("c.txt","r");
	fpw = fopen("d.txt","w");
	if (fpr == NULL) {
		printf("文件打开失败!");
	}
	if (fpw == NULL) {
		printf("文件打开失败!");
	}
	int ch;
	while (1) {
		ch = fgetc(fpr);
		if (ch==EOF) {//注意这里是EOF 不是NULL!
			break;
		}
		fputc(ch, fpw);//直接在文件中写入 不会输出在终端上!
	}
	fclose(fpr);
	fclose(fpw);
	fpr = NULL;
	fpw = NULL;
	return 0;
}

fgets();fputs();

#include <stdio.h>
#include <stdlib.h>
int main() {
	FILE* fpr = NULL;
	FILE* fpw = NULL;
	fpr = fopen("e.txt","r");
	fpw = fopen("f.txt","w");
	char str[10] = {0};
	fgets(str, 5, fpr);
	fputs(str, fpw);
	fclose(fpr);
	fclose(fpw);
	fpr = NULL;
	fpw = NULL;
	return 0;
}

二进制文件 fread();以及fwrite();

#include <stdio.h>
#include <stdlib.h>
int main() {
	FILE* fp = NULL;
	fp = fopen("g.txt","w+");
	if (fp == NULL) {
		printf("打开文件失败!\n");
	}
	int a[] = {1,2,3,4,5,6,7,8};
	int b[8] = { 0 };
	int len = sizeof(a) / sizeof(a[0]);
	int size = fwrite(a, sizeof(int), len, fp);//先写后读!
	printf("写入到的size=%d\n", size);
	//相当于fseek(fp,0, SEEK_SET);
	rewind(fp);//没有这个的话 文件即将要读取的位置在八哪里 将可能会发生越界 造成程序崩溃都有可能!这个的功能:将定位弄掉开头!
	 size=fread(b,sizeof(int),9,fp);
	printf("读取到的size=%d\n",size);
	for (int i = 0;i<size;i++) {
		printf("%d ",b[i]);
	}
	
	return 0;
}

fseek函数的应用:

#include <stdio.h>
#include <stdlib.h>
int main() {
	FILE* fp = NULL;
	fp = fopen("g.txt","w+");
	if (fp == NULL) {
		printf("打开文件失败!\n");
	}
	int a[] = {1,2,3,4,5,6,7,8};
	int b[8] = { 0 };
	int len = sizeof(a) / sizeof(a[0]);
	int size = fwrite(a, sizeof(int), len, fp);//先写后读!
	printf("写入到的size=%d\n", size);
	//相当于fseek(fp,0, SEEK_SET);
	rewind(fp);//没有这个的话 文件即将要读取的位置在八哪里 将可能会发生越界 造成程序崩溃都有可能!这个的功能:将定位弄掉开头!
	 size=fread(b,sizeof(int),9,fp);
	printf("读取到的size=%d\n",size);
	for (int i = 0;i<size;i++) {
		printf("%d ",b[i]);
	}
		//fseek函数
    int c[2] = { 0 };
	fseek(fp, 8, SEEK_SET);//从文件开头往后八个字节开始操作文件,即在3开始输出3,4
	size = fread(c, sizeof(int), 2, fp);
	printf("\n");
	for (int i = 0; i < size; i++) {
		printf("%d ", c[i]);
	}
	fseek(fp, 8, SEEK_CUR);//从文件当前定位位置(5)向后移动八个字节开始操作文件 输出7 8
	size = fread(c, sizeof(int), 2, fp);
	printf("\n");
	for (int i = 0; i < size; i++) {
		printf("%d ", c[i]);
	}
	fseek(fp, -12, SEEK_END);//从文件末尾位置开始向前移动12个字节开始操作文件 6,7
	size = fread(c, sizeof(int), 2, fp);
	printf("\n");
	for (int i = 0; i < size; i++) {
		printf("%d ", c[i]);
	}

	fclose(fp);
	fp = NULL;
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值