fp
有以下程序
#include
main()
{
FILE *fp;
int k, n, a[6]={1,2,3,4,5,6};
fp = fopen("d2.dat", "w");
fprintf(fp, "%d%d%d\n", a[0],a[1],a[2]);
fprintf(fp, "%d%d%d\n", a[3],a[4],a[5]);
fclose(fp);
fp = fopen("d2.dat", "r");
fscanf(fp, "%d%d", &k, &n);
printf("%d %d\n", k, n);
fclose(fp);
}
程序运行后的输出结果是
有以下程序
#include
main()
{ FILE *fp;
int a[10]={1,2,3,0,0}, i;
fp = fopen("d2.dat", "wb");
fwrite(a, sizeof(int), 5, fp);
fwrite(a, sizeof(int), 5, fp);
fclose(fp);
fp = fopen("d2.dat", "rb");
fread(a, sizeof(int), 10, fp);
fclose(fp);
for (i=0; i<10; i++)
printf("%d,", a[i]);
}
程序的运行结果是
若有以下程序
#include
main()
{ FILE *fp;
int i, a[6]={1,2,3,4,5,6},k;
fp = fopen("data.dat", "w+");
for (i=0; i<6; i++)
{ fseek(fp, 0L, 0); fprintf(fp, "%d\n", a[i]); }
rewind(fp);
fscanf(fp, "%d", &k);
fclose(fp);
printf("%d\n", k);
}
则程序的输出结果是
若有以下程序
#include
main()
{ FILE *fp;
int i, a[6]={1,2,3,4,5,6}, k;
fp = fopen("data.dat", "w+b");
for (i=0; i<6; i++)
{ fseek(fp, 0L, 0);
fwrite(&a[5-i], sizeof(int), 1, fp);
}
rewind(fp);
fread(&k, sizeof(int), 1, fp);
fclose(fp);
printf("%d", k);
}
则程序的输出结果是