(求助)文件打不开是怎么回事?
#include "stdio.h"#include "string.h"
#define MAXN 300
#define MAX_NAME 20
int len;
typedef struct student{
int num;
char name[MAX_NAME];
int english;
int math;
int computer;
} STUDENT;
/*-----------input sub program---------*/
input(STUDENT stu[])
{
int no;
char ch[MAX_NAME];
int i=0 ;
putchar('\n');
printf("%s\n", "Enter the new records.And if you want exit, enter -1.");
printf("%s\n", "format: number");
printf("%s\n", " name english math computer");
putchar('\n');
printf("such as:\n");
printf("1\n");
printf("Gates 100 100 100 100 \n");
while(i<MAXN){
printf("please input code :") ;
scanf("%d", &no);
if(no==-1)
{len=i;
break;
}
while(no !=0) {
stu[i].num = no;
scanf("%s%d%d%d", stu[i].name, &stu[i].english, &stu[i].math,&stu[i].computer);
i++;
scanf("%d",&no);
}
}
}
/*---------------output sub program---------------*/
void output(STUDENT stu[])
{
int i;
clrscr();
printf("%8s", "number");
printf("%8s", "name");
printf("%8s", "english");
printf("%8s", "math");
printf("%10s", "computer");
putchar('\n');
for (i =0; i < 80; i++)
putchar('=');
putchar('\n');
for (i = 0; i< len; i++)
{
printf("%8d", stu[i].num);
printf("%8s", stu[i].name);
printf("%8d", stu[i].english);
printf("%8d", stu[i].math);
printf("%10d", stu[i].computer);
putchar('\n');
}
for (i =0; i < 80; i++)
putchar('=');
putchar('\n');
printf("Press enter to continue.");
getchar();
getchar();
}
/*--------------------find sub program-----------------------*/
find(STUDENT stu[])
{
int find_no, result ;
int i;
lab: result=0;
printf("%s\n", "Enter the number which you want to find. If you want exit, enter -1.");
scanf("%d", &find_no);
if (find_no == -1) exit(1); /*exit the fine sub program*/
while( stu[result].num != find_no && result < len) result ++;
if (result >= len ) {
printf("%s\n", "There is no the record which you want.");
goto lab;
}
else {
clrscr();
printf("%s\n", "The flowing record is you want.");
for (i =0; i < 80; i++)
putchar('=');
putchar('\n');
printf("%8s", "number");
printf("%8s", "name");
printf("%8s", "english");
printf("%8s", "math");
printf("%10s", "computer");
putchar('\n');
printf("%8d", stu[result].num);
printf("%8s", stu[result].name);
printf("%8d", stu[result].english);
printf("%8d", stu[result].math);
printf("%10d", stu[result].computer);
putchar('\n');
for (i =0; i < 80; i++)
putchar('=');
putchar('\n');
goto lab;
}
}
/*-----------------insert sub program-------------*/
insert(STUDENT stu[])
{
int no, pos, english, math, computer, i;
char name[MAX_NAME];
lab: printf("%s\n", "Enter the new records. If you want exit, enter -1.");
printf("%s\n", "format: number");
printf("%s\n", " name english math computer average total");
scanf("%d", &no);
if (no == -1) return ;
stu[len].num = no;
scanf("%s%d%d%d", name,&english,&math,&computer);
pos = 0;
while ((stu[pos].math < math) && (pos < len) )
pos ++;
for (i = len-1; i >= pos; i--)
stu[i+1] = stu[i];
stu[pos].num = no;
strcpy(stu[pos].name, name);
stu[pos].english = english;
stu[pos].math = math;
stu[pos].computer = computer;
len=len+1;
goto lab;
}
/*--------------delete sub program--------------*/
delete(STUDENT stu[])
{
int no, i, pos;
lab: pos=0;
printf("%s\n", "Enter the number which you want to delete. If you want exit, enter -1.");
scanf("%d", &no);
if (no == -1) return;
while( (stu[pos].num != no) && (pos < len) )
pos = pos +1;
if (pos >= len) {
printf("%s\n", "There is no the record which you want.");
goto lab;
}
else {
for (i = pos+1;i < len; i++)
stu[i-1] = stu[i];
len = len -1;
if (len == 0) {
printf("%s\n", "The number of records is zero, press enter to return.");
getchar();
getchar();
return;
}
goto lab;
}
}
/*------------write--------------*/
write(STUDENT stu[])
{ FILE *ft;
if((ft=fopen("c:\\stu.dat","wb"))==NULL)
{printf("!!error\n");
}
if(fwrite(stu,sizeof(STUDENT),len,ft)!=len)
{printf("error\n");}
fclose(ft);
}
/*------------load-------------*/
load(STUDENT stu[])
{
int i;
FILE *ft;
if((ft=fopen("c:\\stu.dat","rb"))==NULL)
{printf("read error!");
}
for(i=0;i!=feof(ft);i++)
{fread(&stu[i],sizeof(struct student),1,ft);
len++;
}
return len ;
}
/*---------------manu------------*/
void paint()
{
int i;
clrscr();
printf("%43s\n", "Manu");
for (i = 0; i < 80; i++)
putchar('=');
putchar('\n');
printf(" 1 input 2 output\n");
printf(" 3 find(number) 4 insert \n");
printf(" 5 delete(number) 6 write \n");
printf(" 7 read 0 quit\n");
for (i = 0; i<= 79; i++)
putchar('=');
putchar('\n');
printf("%s\n", "Please enter the command:");
}
/*------------main program---------------*/
main()
{
STUDENT stu[MAXN];
char ctrl_ch;
len=read(stu);
paint();
scanf("%c", &ctrl_ch);
while (ctrl_ch != '0') {
switch(ctrl_ch) {
case '1':
input(stu);
break;
case '2':
output(stu);
break;
case '3':
find(stu);
break;
case '4':
insert(stu);
break;
case '5':
delete(stu);
break;
case '6':
write(stu);
break;
case '7':
load(stu);
break;
default:
if (ctrl_ch != '\n') printf("%s\n", "Error command.");
break;
}
if (ctrl_ch != '\n') paint();
scanf("%c", &ctrl_ch);
}
}