請大佬幫忙看一下這段代碼,為什么檔案輸入總是錯的,怎么改正
void input()//cin the student information
{
void input1();
if(d==0)//the first time
{
for(i=1;i<=n;i++)
input1();
printf("\n OK,it is done.\n\n");
}
else if(d==1)//add the information
{
for(i=n+1;i<=n+flag;i++)
input1();
}
n=n+flag;
printf("\n OK,it is done.I will return\n\n");
}
void input1()
{
FILE * fp;
fp=fopen("D:\\CC\\grades.txt","r");
if(fp==NULL)
{
printf("cannot find the file!!!\n");
exit(0);
}
while(!feof(fp))
{
printf("please cin number%d's information:\n",i);
printf("the school number:\t");
for(int k=0;k<i;k++)
{
fscanf(fp,"%s",&grades[i].num);
if(grades[i].num<=0)
{
printf("Error! Please check it again\n");
fscanf(fp,"%s",&grades[i].num);
}
}
for(j=1;j<i;j++)
{
if(strcmp(grades[i].num,grades[j].num)==0)
{
printf("the two are same Try it again");
fscanf(fp,"%s",&grades[i].num);
break;
}
}
printf("name:\t");
fscanf(fp,"%s",&grades[i].name);
printf("the sex 1 for a boy and 0 for a girl:");
fscanf(fp,"%d",&grades[i].sex);
if(grades[i].sex!=1&&grades[i].sex!=2)
{
printf("We don't have a shemale ,try it again");
fscanf(fp,"%d",&grades[i].sex);
}
printf("the height:\t");
fscanf(fp,"%f",&grades[i].height);
if(grades[i].height<=40||grades[i].height>=250)
{
printf("The data maybe wrong,please try it again");
fscanf(fp,"%f",&grades[i].height);
}
printf("the weight:\t");
fscanf(fp,"%f",&grades[i].weight);
if(grades[i].weight<=40||grades[i].weight>=300)
{
printf("The data maybe wrong,please try it again");
fscanf(fp,"%f",&grades[i].weight);
}
printf("FCV:\t");
fscanf(fp,"%d",&grades[i].FCV);
if(grades[i].FCV<=300||grades[i].FCV>=9000)
{
printf("The data maybe wrong,please try it again");
fscanf(fp,"%d",&grades[i].FCV);
}
printf("UPS or WAKES:\t");
fscanf(fp,"%d",&grades[i].count);
if(grades[i].count<0||grades[i].count>=65)
{
printf("The data maybe wrong,please try it again");
fscanf(fp,"%d",&grades[i].count);
}
if (grades[i].sex==1) {
if (grades[i].FCV>4800) {
grades[i].sco1=100;
}else if (grades[i].FCV>4180) {
grades[i].sco1=80;
}else if (grades[i].FCV>3100) {
grades[i].sco1=60;
}else grades[i].sco1=30;
if (grades[i].count>19) {
grades[i].sco2=100;
}else if (grades[i].count>15) {
grades[i].sco2=80;
}else if (grades[i].count>9) {
grades[i].sco2=60;
}else grades[i].sco2=30;
}else if (grades[i].sex==2) {
if (grades[i].FCV>=3400) {
grades[i].sco1=100;
}else if (grades[i].FCV>3000) {
grades[i].sco1=80;
}else if (grades[i].FCV>2050) {
grades[i].sco1=60;
}else grades[i].sco1=30;
if (grades[i].count>56) {
grades[i].sco2=100;
}else if (grades[i].count>52) {
grades[i].sco2=80;
}else if (grades[i].count>24) {
grades[i].sco2=60;
}else grades[i].sco2=30;
}
grades[i].BMI=grades[i].weight/(grades[i].height*grades[i].height);
if (grades[i].BMI>=17.9&&grades[i].BMI<=23.9) {
grades[i].sco3=100;
}else if (grades[i].BMI<17.9) {
grades[i].sco3=80;
}else if(grades[i].BMI>=24.0&&grades[i].BMI<=27.9){
grades[i].sco3=80;
}else if (grades[i].FCV>27.9)
grades[i].sco3=60;
grades[i].scoALL=0.25*grades[i].sco3+0.35*grades[i].sco1+0.40*grades[i].sco2;
if (grades[i].scoALL>=95)
{
strcpy(grades[i].level, "excellent");
}
else if (grades[i].scoALL>=80)
{
strcpy(grades[i].level, "good");
}
else if (grades[i].scoALL>=60)
{
strcpy(grades[i].level, "pass");
}
else strcpy(grades[i].level, "fail");
}
fclose(fp);
}
void save()//save the information
{
FILE *fp;
if((fp = fopen("student.txt" , "w")) == NULL )// open the txt
{
printf("the file cannot open\n");
exit(1);
}
int i = 0;
while(i < n)
{
i++;
if(i < n)
fprintf(fp, "\n");
}
fclose(fp);
}

轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/245425.html
標籤:C語言
上一篇:指標 結構體與共用體 列舉型別
