c语言实现文件的读写操作

 c语言实现文件的读写操作:

#include<stdio.h>
#include<stdlib.h>
#define N 5
typedef struct {
	int number;//学号
	char name[30];//姓名
	char major[30];//专业
} Student_info; 
int main(){
	
	//写文件 
//	FILE *fp=fopen("D:/test.txt","w");
//	if(fp==NULL){
//		printf("打开文件失败!");
//		exit(0);
//	}
//	for(int i=0;i<10;i++){
//		fputs("Hello file!",fp);
//	}
//	fclose(fp);
	
	//读文件 
//	FILE *fp1=fopen("D:/test.txt","r");
//	if(fp1==NULL){
//		printf("打开文件失败!");
//		exit(0);
//	}
//	fseek(fp1,15,SEEK_SET);
//	char c=fgetc(fp1);
//	printf("字符%c",c);
//	fclose(fp1); 

//	//以"写"方式打开文件
//	FILE *fp=fopen("D:/test.txt","w");
//	if(fp==NULL) {
//		printf("打开文件失败!");
//		exit(0);
//	}
//	Student_info student[N]; //用数组保存N个学生信息
//	for(int i=0;i<N;i++) { //生成 N个学生信息
//		student [i].number=i;
//		student [i].name[0]='?';
//		student[1].major[0]='?';
//		printf("\n");
//	}
//	//将N个学生的信息写入文件
//	fwrite(student,sizeof(Student_info),N,fp);
//	fclose(fp);

	//以"读"方式打开文件
	FILE *fp1=fopen("D:/test.txt","r");
	if(fp1==NULL) {
		printf("打开文件失败!");
		exit(0);
	}
	//文件读写指针指向编号为5的学生记录
	fseek(fp1,5*sizeof(Student_info),SEEK_SET); 
	Student_info stu; //用数组保存N个学生信息
	//将文件读出1条记录,记录大小为sizeof(Student_info) 
	fread(&stu,sizeof(Student_info),1,fp1);
	printf("学生编号:%d\n",stu.number);
	fclose(fp1);
} 

  • 2
    点赞
  • 22
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值