Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- データの書き込みのところでアクセスしてはいけない
- メモリにアクセスしていたので、
- for文に直しました。基本的には
- いじっていません。
- */
- #include<stdio.h>
- #include<stdlib.h>
- /* ファイルからデータを読み込み、平均点を算出 */
- struct student{
- int id;
- char name[20];
- int kokugo;
- int sugaku;
- int eigo;
- };
- int main(void){
- FILE *fp;
- struct student student1[3];
- int i = 0;
- double ave[3]; //平均点
- //ファイルのオープン
- if((fp = fopen("seiseki2.csv", "r")) == NULL){
- printf("error");
- exit(1);
- }
- //データの読み込み
- while((fscanf(fp, "%d,%[^,],%d,%d,%d", &student1[i].id, student1[i].name,
- &student1[i].kokugo, &student1[i].sugaku, &student1[i].eigo)) != EOF){
- i++;
- }
- fclose(fp);
- //平均点の計算
- for(i = 0; i < 3; i++){
- ave[i] = (double)(student1[i].kokugo + student1[i].sugaku + student1[i].eigo) / 3;
- }
- //ファイルのオープン
- if((fp = fopen("average.txt", "w")) == NULL){
- printf("error");
- exit(1);
- }
- //データの書き込み
- for(i = 0; i < 3; i++){
- fprintf(fp, "%d,%s,%.2lf\n", student1[i].id, student1[i].name, ave[i]);
- }
- fclose(fp);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment