用C语言编程,我想打开一个文本文件,如果不存在,则新建一个文件;

2020-06-18 教育 213阅读
示例程序:
#include
#include
#define M 4
#define N 5
int fun (int a[M][N])
{
int i,j,sum=0;
for (i=0;i {
for (j=0;j sum=sum+a[i][j];
}
return(sum);
}
void main()
{
int aa[M][N]={{1,3,5,7,9},{2,9,9,9,4},{6,9,9,9,8}, {1,3,5,7,0}};
int i, j, y;
void NONO ();
printf ("The original data is : \n");
for (i=0; i {
for (j =0; j printf("%6d", aa[i][j]);
printf ("\n");
}
y = fun (aa);
printf("\nThe sum: %d\n" , y);
printf("\n");
NONO();
}
void NONO ()
{/* 请在此函数内打开文件,输入测试数据,调用 fun 函数,输出数据,关闭文件。 */
int i, j, y, k, aa[M][N] ;
FILE *rf, *wf ;
rf = fopen("in.dat","r");
wf = fopen("out.dat","w");
for(k = 0 ; k < 10 ; k++) {
for(i = 0 ; i < M ; i++)
for(j = 0 ; j < N ; j++)
fscanf(rf,"%d", &aa[i][j]);
y = fun (aa);
fprintf(wf,"%d\n", y);
}
fclose(rf);
fclose(wf);
}
声明:你问我答网所有作品(图文、音视频)均由用户自行上传分享,仅供网友学习交流。若您的权利被侵害,请联系fangmu6661024@163.com