Professional Documents
Culture Documents
Chuong 9
Chuong 9
FILE
I.
1.
a file ra a:
M file:
Muc ich cua phn nay la xy dng
mt file mi ghi thng tin ln o hoc
khi ng mt file a tn tai b
sung thm d liu.
m mt file ta s dung ham
fopen()
3. ong file
#include "stdio.h"
main()
{
FILE *fpp, *fopen();
int a[4][4];
int i,j,m;
fpp=fopen("matran","w");
for (i=0;i<4;++i)
for (j=0;j<4;++j)
{
printf("a[%d,%d]= ",i,j);
scanf("%d",&m);
a[i][j]=m;
}
for (i=0;i<4;++i)
for (j=0;j<4;++j)
fprintf(fpp,"%d\n",a[i][j]);
fclose(fpp);
return 0;
}
II. oc vao t file
1. M file
S dung ham fopen()
Cu phap hoan toan ging phn trn ch
khac la phn type la r
V du:
Vit lai chng trnh v du trn , sau
o oc file va in ra man hnh.
#include "stdio.h"
main()
{
FILE *fpp, *fopen();
int a[4][4];
int i,j,m;
fpp=fopen("matran","w");
for (i=0;i<4;++i)
for (j=0;j<4;++j)
{
printf("a[%d,%d]= ",i,j);
scanf("%d",&m);
a[i][j]=m;
}
for (i=0;i<4;++i)
for (j=0;j<4;++j)
fprintf(fpp,"%d\n",a[i][j]);
fclose(fpp);
fpp=fopen("matran","r");
for (i=0;i<4;++i)
{
for (j=0;j<4;++j)
{
fscanf(fpp,"%d",&m);
printf("%d ",m);
}
printf("\n");
}
getch();
return 0;
}