求c语言编程,,帮帮忙,,急急,,急急!!!!

2024-11-08 12:31:42
推荐回答(1个)
回答1:

我做过一个类似的 只是数据结构不一样(但也是学生的)
你看懂了这个 你就自己会编了
#include
#include
#include
int M=3;//用户数
int N=7;//学生数

struct birthday
{
int year;
int month;
int day;
};
struct score
{
int English;
int Math;
int Clanguage;
};
struct student//学生
{
char academy[10];
char remove[10];
int number;
char name[10];
char sex;
char adress[20];
struct birthday birday;
struct score grade;
}stu[50];

struct student p[50];

struct user//用户
{
char name[10];
char password[10];
int GF;
}use[10];

void bbb();/*初始化用户信息存在myfile1.txt*/
void aaa();/*初始化学生信息myfile.txt*/
void ccc();/*初始化学生与用户的个数*/
void startget();/*将文件的数据读入到数组中use[10],p[50]*/

void main()
{
aaa();
bbb();
ccc();
printf("安装完成!\n");
printf("按回车键结束!\n");
getchar();
startget();
printf("初始化话成功!\n");
printf("按回车键结束!\n");
getchar();
/*****下面你就可以用数组use[10],p[50]了,数据初始化了***********/
//////////////////////////////////////////////////////////////////
}
void aaa()
{
struct student stu[50]={{"计算机","软件041",1,"张东",'m',"北京",{87,8,7},{80,90,85}},
{"计算机","软件041",2,"于南",'m',"上海",{86,11,7},{98,80,95}},
{"计算机","软件041",3,"周西",'f',"杭州",{87,4,30},{45,78,79}},
{"计算机","软件042",4,"王北",'m',"广东",{86,5,30},{76,71,74}},
{"计算机","软件042",5,"李中",'m',"南京",{85,3,30},{60,69,79}},
{"人文","法律041",6,"顾君",'f',"海南",{84,9,21},{85,83,54}},
{"水院","土木043",7,"陈海",'m',"天津",{88,8,5},{97,91,86}}};
FILE *pf;
if((pf=fopen("d:\\myfile.txt","w"))==0)//建立文件
{
printf("Can'open it");
exit(0);
}
for(int i=0;i if(fwrite(stu+i,sizeof(struct student),1,pf)!=1)
printf("File write error\n");
fclose(pf);
;}

void bbb()
{
struct user use[10]={{"yudonghao","2",1},{"masong","1",-1},{"yudong","3",-1},};
FILE *pf;
int i;
if((pf=fopen("d:\\myfile1.txt","w"))==NULL)//建立文件
{
printf("Can't open it");
exit(0);
}
for(i=0;i if(fwrite(use+i,sizeof(struct user),1,pf)!=1)
printf("File write error\n");
fclose(pf);
}
void ccc()
{
int b[2];
b[0]=N;
b[1]=M;
FILE *pf;
if((pf=fopen("d:\\myfile2.txt","w"))==NULL)//建立文件
{
printf("Can't open it");
exit(0);
}
if(fwrite(b,sizeof(int),2,pf)==0)
printf("File write error\n");
fclose(pf);
}

void startget()
{
int b[2];
FILE *pf,*pf1,*pf2;
if((pf=fopen("D:\\myfile2.txt","r"))==NULL)//以读形式打开文件
{
printf("Can'open it");
exit(0);
}
fread(b,sizeof(int),2,pf);
fclose(pf);

N=b[0],M=b[1];

if((pf1=fopen("D:\\myfile.txt","r"))==NULL)//以读形式打开文件
{
printf("Can'open it");
exit(0);
}
for(int i=0;i fread(p+i,sizeof(struct student),1,pf1);
fclose(pf1);

if((pf2=fopen("D:\\myfile1.txt","r"))==NULL)//以读形式打开文件
{
printf("Can'open it");
exit(0);
}
fread(use,sizeof(struct user),M,pf2);
fclose(pf2);
}