三個程式,1、2沒問題,第3個無法讀取保存的資料檔案
先是前兩個沒問題的
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#define N 5
struct student
{
int id;
float score[3];
float ave;
char name[10];
}
stu[N];
int main()
{
void save(void);
int i;
FILE *fp1;
for(i=0;i<N;i++)
{
printf("請輸入第%d名學生的學號,姓名:",i+1);
scanf("%d,%s",&stu[i].id,stu[i].name);
printf("請輸入該學生的三門課成績:\n",i+1);
scanf("%f,%f,%f",&stu[i].score[0],&stu[i].score[1],&stu[i].score[2]);
stu[i].ave=(stu[i].score[0]+stu[i].score[1]+stu[i].score[2])/3;
}
save();
fp1=fopen("stud","rb");
printf("學號,姓名,成績1,成績2,成績3,平均成績\n");
for(i=0;i<N;i++)
{
fread(&stu[i],sizeof(struct student),1,fp1);
printf("%5d %s %3.1f %3.1f %3.1f %3.2f\n",stu[i].id,stu[i].name,stu[i].score[0],stu[i].score[1],stu[i].score[2],stu[i].ave);
}
fclose(fp1);
return 0;
}
void save(void)
{
FILE *fp;
int i;
if((fp=fopen("stud","wb"))==NULL)
{
printf("檔案不能打開\n");
return;
}
for(i=0;i<N;i++)
if(fwrite(&stu[i],sizeof(struct student),1,fp)!=1)
{
printf("檔案寫入錯誤\n");
return;
}
fclose(fp);
}
#include <stdio.h>
#include <stdlib.h>
#define N 5
struct student
{
int id;
float score[3];
float ave;
char name[10];
}
stu[N],stu0;
int main()
{
FILE *fp1,*fp2;
int i,j;
if((fp1=fopen("stud","rb"))==NULL)
{
printf("檔案不能打開\n");
return 0;
}
for(i=0;i<N;i++)
{
fread(&stu[i],sizeof(struct student),1,fp1);
}
fclose(fp1);
for(i=0;i<N-1;i++)
{
for(j=0;j<N-i-1;j++)
if(stu[j].ave<stu[j+1].ave)
{
stu0=stu[j];
stu[j]=stu[j+1];
stu[j+1]=stu0;
}
}
if((fp2=fopen("stud_sort","rb"))==NULL)
{
printf("檔案不能打開\n");
return 0;
}
printf("學號,姓名,成績1,成績2,成績3,平均成績\n");
for(i=0;i<N;i++)
{
fread(&stu[i],sizeof(struct student),1,fp2);
printf("%5d %s %3.1f %3.1f %3.1f %3.2f\n",stu[i].id,stu[i].name,stu[i].score[0],stu[i].score[1],stu[i].score[2],stu[i].ave);
}
fclose(fp2);
return 0;
}
void save(void)
{
FILE *fp;
int i;
if((fp=fopen("stud_sort","wb"))==NULL)
{
printf("檔案不能打開\n");
return ;
}
for(i=0;i<N;i++)
if(fwrite(&stu[i],sizeof(struct student),1,fp)!=1)
{
printf("檔案寫入錯誤\n");
return ;
}
fclose(fp);
}
第三個
#include <stdio.h>
#include <stdlib.h>
#define N 5
struct student
{
int id;
float score[3];
float ave;
char name[10];
}
stu[N],stu0;
int main()
{
FILE *fp1,*fp2;
int i,j;
if((fp1=fopen("stud_sort","rb"))==NULL)
{
printf("檔案不能打開\n");
return 0;
}
for(i=0;i<N;i++)
{
fread(&stu[i],sizeof(struct student),1,fp1);
}
fclose(fp1);
printf("請輸入新的學生學號,姓名:");
scanf("%d,%s",&stu[i].id,stu[i].name);
printf("請輸入三門課成績:\n",i+1);
scanf("%f,%f,%f",&stu[5].score[0],&stu[5].score[1],&stu[5].score[2]);
stu[5].ave=(stu[5].score[0]+stu[5].score[1]+stu[5].score[2])/3;
for(i=4;i>0;i--)
stu[5]=stu0;
if(stu[i].ave<stu[5].ave)
{
stu[i]=stu[i+1];
stu[0]=stu[i];
}
if((fp2=fopen("stud_new","rb"))==NULL)
{
printf("檔案不能打開\n"); \\這里出了問題
return 0;
}
printf("學號,姓名,成績1,成績2,成績3,平均成績\n");
for(i=0;i<N;i++)
{
fread(&stu[i],sizeof(struct student),1,fp2);
printf("%5d %s %3.1f %3.1f %3.1f %3.2f\n",stu[i].id,stu[i].name,stu[i].score[0],stu[i].score[1],stu[i].score[2],stu[i].ave);
}
fclose(fp2);
return 0;
}
void save(void)
{
FILE *fp;
int i;
if((fp=fopen("stud_new","wb"))==NULL)
{
printf("檔案不能打開\n");
return ;
}
for(i=0;i<N;i++)
if(fwrite(&stu[i],sizeof(struct student),1,fp)!=1)
{
printf("檔案寫入錯誤\n");
return ;
}
fclose(fp);
}
1、有5個學生,每個學生有3門課程的成績,從鍵盤輸入學生資料(包括學號,姓名,3門課程成績),/計算出平均成績,將原有資料和計算出的平均分數存放在磁盤檔案"stud"中。
2、將第1題"stud"檔案中的學生資料,按平均分進行排序處理,將已排序的學生資料存入一個新檔案"stud_sort"中。
3、將第2題已排序的學生成績檔案進行插入處理。插入一個學生的3門課程成績,程式先計算新插入學生的平均成績,然后將它按成績高低順序插入,插入后建立一個新檔案stud_new。
uj5u.com熱心網友回復:
aaaaas轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/59037.html
標籤:C語言
