今年单招的学校:完善我的C程序 谢谢

来源:百度文库 编辑:中科新闻网 时间:2024/05/08 23:43:29
我编写了一段C程序,运行成功
我的目的是将我输入的内容保存到C盘的std.dat文件中
但是我对保存文件不是很清楚
希望高人帮我在我的程序后面写一段保存的代码
感谢!!!!
急~~~

#include<stdio.h>
#include<string.h>
#include<conio.h>
#define N 100

struct student
{ char num[10];char name[10];char math[3];char english[3];char language[3];char dianlu[3];char law[3];};

void print();
void create(struct student *p,int *n);

main()
{ char choose,yes_no;
struct student record[N];
int n=0;

do
{ print();
printf(" ");
choose=getch();
switch(choose)
{ case'1':create(record,&n); break;
case'2':
if(n==0)
{printf("\n norecord!\n");
break;}
/*display(record,n);
break; */
case'3':
if(n==0)
{printf("\n norecord!\n");
break;}
/*deal(record,&n);
break; */
case'0':break;
default:printf("\n input right number!\n");
}
if(choose=='0') break;
printf("\n go on choosing(Y/N)?\n");
do
{ yes_no=getche();
} while(yes_no!='Y'&&yes_no!='y'&&yes_no!='N'&&yes_no!='n');
}while(yes_no=='Y'||yes_no=='y');
}

void print()
{ printf("\n\n\n\n\n\n");
printf(" |-------------------------|\n");
printf(" | Please choose(0---3)---|\n");
printf(" |-------------------------|\n");
printf(" | 1--input |\n");
printf(" | 2--tongji |\n");
printf(" | 3--deal |\n");
printf(" | 0--exit |\n");
printf(" |-------------------------|\n");
}

void create(struct student *p,int *n)
{ struct student temp;

printf("\n input NO.1:\n");
printf(" num(use '#' end):");
do
{ gets(temp.num);
} while(strcmp(temp.num,"")==0);
printf(" name(use '#' end):"); gets(temp.name);
printf(" math(use '#' end):"); gets(temp.math);
printf(" english(use '#' end):"); gets(temp.english);
printf(" language(use '#' end):"); gets(temp.language);
printf(" dianlu(use '#' end):"); gets(temp.dianlu);
printf(" law(use '#' end):"); gets(temp.law);

while(temp.num[0]!='#'&&temp.name[0]!='#'&&temp.math[0]!='#'&&temp.english[0]!='#'&&temp.language[0]!='#'&&temp.dianlu[0]!='#'&&temp.law[0]!='#')
{
*p=temp;
p++; (*n)++;

printf("\n input NO.%d record:\n",(*n)+1);
printf(" num(use '#' end):");
do
{ gets(temp.num);
} while(strcmp(temp.num,"")==0);
printf(" name(use '#' end):"); gets(temp.name);
printf(" math(use '#' end):"); gets(temp.math);
printf(" english(use '#' end):"); gets(temp.english);
printf(" language(use '#' end):"); gets(temp.language);
printf(" dianlu(use '#' end):"); gets(temp.dianlu);
printf(" law(use '#' end):"); gets(temp.law);
}
}

#include<stdio.h>
#include<string.h>
#include<conio.h>
#define N 100

struct student
{ char num[10];char name[10];char math[3];char english[3];char language[3];char dianlu[3];char law[3];};

void print();
void create(struct student *p,int *n,FILE *fp);

int main()
{
FILE *fp;
if((fp=fopen("c:\\std.dat","w"))==NULL)
{ printf("不能创建文件c:\\std.dat!"); return 0;
}
char choose,yes_no;
struct student record[N];
int n=0;

do
{ print();
printf(" ");
choose=getch();
switch(choose)
{ case'1':create(record,&n,fp); break;
case'2':
if(n==0)
{printf("\n norecord!\n");
break;}
/*display(record,n);
break; */
case'3':
if(n==0)
{printf("\n norecord!\n");
break;}
/*deal(record,&n);
break; */
case'0':break;
default:printf("\n input right number!\n");
}
if(choose=='0') break;
printf("\n go on choosing(Y/N)?\n");
do
{
yes_no=getche();
}
while(yes_no!='Y'&&yes_no!='y'&&yes_no!='N'&&yes_no!='n');
}
while(yes_no=='Y'||yes_no=='y');
fclose(fp);return 1;
}

void print()
{ printf("\n\n\n\n\n\n");
printf(" |-------------------------|\n");
printf(" | Please choose(0---3)---|\n");
printf(" |-------------------------|\n");
printf(" | 1--input |\n");
printf(" | 2--tongji |\n");
printf(" | 3--deal |\n");
printf(" | 0--exit |\n");
printf(" |-------------------------|\n");
}

void create(struct student *p,int *n,FILE *fp)
{ struct student temp;

printf("\n input NO.1:\n");
printf(" num(use '#' end):");
do
{ gets(temp.num); fprintf(fp,"num:%s ",temp.num);
} while(strcmp(temp.num,"")==0);
printf(" name(use '#' end):"); gets(temp.name); fprintf(fp,"%s ",temp.name);
printf(" math(use '#' end):"); gets(temp.math); fprintf(fp,"%s ",temp.math);
printf(" english(use '#' end):"); gets(temp.english); fprintf(fp,"%s ",temp.english);
printf(" language(use '#' end):"); gets(temp.language); fprintf(fp,"%s ",temp.language);
printf(" dianlu(use '#' end):"); gets(temp.dianlu); fprintf(fp,"%s ",temp.dianlu);
printf(" law(use '#' end):"); gets(temp.law); fprintf(fp,"%s \n",temp.law);

while(temp.num[0]!='#'&&temp.name[0]!='#'&&temp.math[0]!='#'&&temp.english[0]!='#'&&temp.language[0]!='#'&&temp.dianlu[0]!='#'&&temp.law[0]!='#')
{
*p=temp;
p++; (*n)++;

printf("\n input NO.%d record:\n",(*n)+1);
printf(" num(use '#' end):");
do
{ gets(temp.num); fprintf(fp,"num:%s ",temp.num);
} while(strcmp(temp.num,"")==0);
printf(" name(use '#' end):"); gets(temp.name); fprintf(fp,"%s ",temp.name);
printf(" math(use '#' end):"); gets(temp.math); fprintf(fp,"%s ",temp.math);
printf(" english(use '#' end):"); gets(temp.english); fprintf(fp,"%s ",temp.english);
printf(" language(use '#' end):"); gets(temp.language); fprintf(fp,"%s ",temp.language);
printf(" dianlu(use '#' end):"); gets(temp.dianlu); fprintf(fp,"%s ",temp.dianlu);
printf(" law(use '#' end):"); gets(temp.law); fprintf(fp,"%s \n",temp.law);

}
}