Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #define size 100
- struct grade //定義grade的資料結構
- {
- int student_id;
- int eng_s,math_s,average;
- };
- struct grade s_grade[size];
- int main(void)
- {
- int i;
- for(i=1;i<=3;i++)
- {
- printf("請輸入學生學號,英文成績,數學成績(以空白隔開)");
- scanf("%d %d %d",&s_grade[i].student_id,&s_grade[i].eng_s,&s_grade[i].math_s);
- }
- FILE *written;
- written = fopen("student.txt","w");
- for (i=1; i<=3; i++)
- {
- fprintf(written,"%d %d %d\n",s_grade[i].student_id,s_grade[i].eng_s,s_grade[i].math_s);
- }
- printf("\n寫檔完成\n");
- fclose(written);
- printf("\n**讀檔**\n");
- printf("學號 英文分數 數學分數 平均 \n");
- int a,b,c;
- FILE *read;
- read = fopen("student.txt","r");
- while((fscanf(read,"%d %d %d\n",&a,&b,&c))!=EOF)
- {
- printf("%d %d %d %d\t",a,b,c,(b+c)/2);
- printf("\n=====================================\n");
- }
- system("pause");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment