C-class Week5.Day17

文件批量读取进内存练习

​ 以stuatschool.txt 读入需求格式的信息

#include<stdio.h>
#include "stu.h"

#define NUM 5

void show(Student* stu,int cnt)
{
	for(int i=0;i<cnt-1;i++)
	{
		printf("姓名:%s 性别:%s 学号:%s 密码:%s 语文成绩:%d 数学成绩:%d 英语成绩:%d\n",stu[i].name,stu[i].sex,stu[i].sid,stu[i].pwd,stu[i].chn,stu[i].math,stu[i].eng);
	}
}

int main(int argc,const char* argv[])
{
	FILE* fp = fopen("./stuatschool.txt","r");
	if(fp == NULL)
	{
		perror("fopen");
		return 1;
	}
	Student stu[256] = {};
	int ret,i=0,jud=1;
	while((ret = fgetc(fp) != EOF))
	{
		if(jud == 1)
		{
			fseek(fp,-1,SEEK_CUR);
			jud = 0;
		}
		fscanf(fp,"%s %s %s %s %d %d %d",stu[i].name,stu[i].sex,stu[i].sid,stu[i].pwd,&stu[i].chn,&stu[i].math,&stu[i].eng);
		i++;
	}
	show(stu,i);
	fclose(fp);
	fp == NULL;
}

文件流替换部分信息练习

​ 用grade.txt 中的信息更新 stuatschool.txt的信息,并且从新写入后者文件,或者选择新建文件。可在cover()中修改文件参数

#include<stdio.h>
#include<string.h>
#include "stu.h"
#include "grade.h"

Student stu[256] = {};
Grade grade[256] = {};
int stui = 0;
int gradei = 0;

//显示学生信息
void showstu(Student* stu,int cnt)
{
	printf("----------学生表----------\n");
	for(int i=0;i<cnt;i++)
	{
		printf("姓名:%s 性别:%s 学号:%s 密码:%s 语文成绩:%d 数学成绩:%d 英语成绩:%d\n",stu[i].name,stu[i].sex,stu[i].sid,stu[i].pwd,stu[i].chn,stu[i].math,stu[i].eng);
	}
}

//显示需要插入成绩,以学号为外键
void showgrade(Grade* grade,int cnt)
{
	printf("----------成绩表----------\n");
	for(int i=0;i<cnt;i++)
	{
		printf("学号:%s 语文:%d 数学:%d 英语:%d\n",grade[i].sid,grade[i].chn,grade[i].math,grade[i].eng);
	}
}

//读取需导入成绩
void readgrade(void)
{
	FILE * fp = fopen("./grade.txt","r");
	if(fp == NULL)
	{
		perror("fopen");
		//return 0;
	}
	//Grade grade[256] = {};
	int ret,i=0,jud=1;
	while((ret = fgetc(fp) != EOF))
	{
		if(jud == 1)
		{
			fseek(fp,-1,SEEK_CUR);
			jud = 0;
		}
		fscanf(fp,"%s %d %d %d",grade[i].sid,&grade[i].chn,&grade[i].math,&grade[i].eng);
		i++;
	}
	gradei = i-1;
	fclose(fp);
	fp == NULL;
}

//读入学生信息
void readstu(void)
{
	FILE * fp = fopen("./stuatschool.txt","r");
	if(fp == NULL)
	{
		perror("fopen");
		//return 0;
	}
	//Grade grade[256] = {};
	int ret,i=0,jud=1;
	while((ret = fgetc(fp) != EOF))
	{
		if(jud == 1)
		{
			fseek(fp,-1,SEEK_CUR);
			jud = 0;
		}
		fscanf(fp,"%s %s %s %s %d %d %d",stu[i].name,stu[i].sex,stu[i].sid,stu[i].pwd,    &stu[i].chn,&stu[i].math,&stu[i].eng);
		i++;
	}
	stui = i-1;
	fclose(fp);
	fp == NULL;
}

//给全局stu、grade赋值
int change(Student* stu,Grade* grade)
{
	int scount = 1,fcount = 0,j;
	for(int i=0;i<gradei;i++)
	{
		for(j=0;j<stui;j++)
		{
			if(strcmp(grade[i].sid,stu[j].sid) == 0)
			{
				printf("匹配成功次数:%d\n",scount++);
				stu[j].chn = grade[i].chn;
				stu[j].math = grade[i].math;
				stu[j].eng = grade[i].eng;
				break;
			}
			else 
			{	
				fcount = 1;
			}
		}
		if(fcount == 1 && j == stui) 
		{
			printf("成绩表的第%d行学生学号不存在,请查看学生表中是否有该学生信息!\n",i+1);
			fcount = 0;
		}
	}
}

//重写stuatschool.txt
void cover(void)
{
	FILE* fp = fopen("./stuatschool.txt","w");
	if(NULL == fp)
	{
		perror("fopen");
	}
	for(int i=0;i<stui;i++)
	{
		fprintf(fp,"%s %s %s %s %d %d %d\n",stu[i].name,stu[i].sex,stu[i].sid,stu[i].pwd,stu[i].chn,stu[i].math,stu[i].eng);
	}
	fclose(fp);
	fp == NULL;
}




int main(int argc,const char* argv[])
{
	readgrade();
	readstu();
	//showstu(stu,stui);
	//showgrade(grade,gradei);
	change(stu,grade);
	printf("stu:%d grade:%d\n",stui,gradei);//记录下 成绩表行数,学生表函数
	cover();
	return 0;
}

MakeFile

CC=gcc
STD=-std=gnu99
BIN=school.bin
FLAG=-Wall -Werror -DDEBUG
OBJ=main.o student.o teacher.o admin.o tools.o surface.o

all:$(OBJ)
	$(CC) $(STD) -o $(BIN) $(OBJ)
	
main.o:main.c surface.h
	$(CC) $(STD) $(FLAG) -c main.c
	
student.o:student.c student.h tools.h surface.h
	$(CC) $(STD) $(FLAG) -c student.c
	
teacher.o:teacher.c teacher.h tools.h surface.h admin.h 
	$(CC) $(STD) $(FLAG) -c teacher.c
	
admin.o:admin.c admin.h teacher.h tools.h surface.h 
	$(CC) $(STD) $(FLAG) -c admin.c
	
tools.o:tools.c tools.h surface.h 
	$(CC) $(STD) $(FLAG) -c tools.c
	
surface.o: surface.c surface.h admin.h teacher.h tools.h student.h
	$(CC) $(STD) $(FLAG) -c surface.c
	
clean:
	rm -rf $(OBJ) $(BIN) 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值