/* 動作保証不可 */
#include <stdio.h>
#include <stdlib.h>
#define NUM 10000
#define MAX_LEN 15
int main(int argc, char **argv){
FILE *fp;
FILE *fp_b;
double sum = 0.0;
char string[MAX_LEN];
int count = 1;
fp = fopen("list.txt","r");
if (fp==NULL){
printf("I could not open a file(list.txt).\n");
return 1;
} else {
printf("I could open a file.(list.txt)\n");
}
fp_b = fopen("newlist.txt","w");
if (fp_b==NULL){
printf("I could not open a file(newlist.txt).\n");
return 1;
} else {
printf("I could open a file(newlist.txt).\n");
}
while ((fgets(string,sizeof(string)-1,fp)) != NULL){
sum += strtod(string,NULL);
if(count==200){
fprintf(fp_b,"%f\n",sum/200.0);
printf("%f\n",sum/200.0);
sum = 0.0;
count = 1;
}
count++;
}
fprintf(fp_b,"%f(%d)\n",(double)sum/count,count);
printf("%f(%d)\n",(double)sum/count,count);
fputs("\0",fp_b);
fclose(fp);
printf("I could close a file(list.txt).\n");
fclose(fp_b);
printf("I could close a file(newlist.txt).\n");
return(0);
}
|