//偶也写了个 //不是很好 //不过可以满足你的要求 //如何实现写入100个实数把这些数据存入date文件中, //并且在main()中调用这个date文件进行100个实数的算法,比如:做加法的程序
#include <stdio.h> #include <stdlib.h>
void writefile(int n); void addfile();
void writefile(int n) { int i; FILE *fp; int num; if((fp=fopen("date.txt","w"))==NULL) { printf("can not open the file!\n"); exit(1); } printf("please enter n numbers:\n"); for(i=0;i<n;i++) { printf("please enter th%d num:\t",i+1); scanf("%d",&num); fprintf(fp,"%d ", num); } fclose(fp); }
void addfile() { int num; int sum; FILE *fp; sum=0; if((fp=fopen("date.txt","r"))==NULL) { printf("can not enter this file!\n"); exit(1); } while(fscanf(fp,"%d",&num)!=EOF) { sum=sum+num; } printf("the result is :\t"); printf("sum=%d\n",sum); fclose(fp); }
int main() { int n; printf("please enter the n:\t"); scanf("%d",&n); writefile(n); addfile(); }