作者在 2010-03-30 21:50:42 发布以下内容
#include<stdio.h>
#include<stdlib.h>
#define NUM 3
main ()
{
FILE *fp1;
char *temp;
int i,j;
struct rec{
char id[10];
char name[15];
char department[15];
}record[NUM];
printf("This programis to show the random file input & out put \n");
if((fp1=fopen("f:\\infile.txt","wb"))==NULL)
{
printf("cannot open file");
exit(1);
}
for(i=0;i<NUM;i++)
{
printf("input id:");
scanf("%s",record[i].id);
printf("input name:");
scanf("%s",record[i].name);
printf("input department:");
scanf("%s",record[i].department);
fwrite(&record[i],sizeof(struct rec),1,fp1);
}
fclose(fp1);
if((fp1=fopen("f:\\infile.txt","rb+"))==NULL)
{
printf("cannot open file");
exit(1);
}
printf("****************************************************\n");
printf("%-10s%-15s%-15s\n","id","name","department");
printf("****************************************************\n");
for(i=0;i<NUM;i++)
{
fread(&record[i],sizeof(struct rec),1,fp1);
printf("%-10s%-15s%-15s\n",record[i].id,record[i].name,record[i].department);
}
fseek(fp1,2*sizeof(struct rec),0);
fwrite(&record[1],sizeof(struct rec),1,fp1);
rewind(fp1);
printf("**********************************************************\n");
printf("%-10s%-15s%-10s\n","id","name","department");
printf("**********************************************************\n");
for(i=0;i<NUM;i++)
{
fread(&record[i],sizeof(struct rec),1,fp1);
printf("%-10s%-15s%-15s\n",record[i].id,record[i].name,record[i].department);
}
fclose(fp1);
scanf("%d",i);
}
#include<stdlib.h>
#define NUM 3
main ()
{
FILE *fp1;
char *temp;
int i,j;
struct rec{
char id[10];
char name[15];
char department[15];
}record[NUM];
printf("This programis to show the random file input & out put \n");
if((fp1=fopen("f:\\infile.txt","wb"))==NULL)
{
printf("cannot open file");
exit(1);
}
for(i=0;i<NUM;i++)
{
printf("input id:");
scanf("%s",record[i].id);
printf("input name:");
scanf("%s",record[i].name);
printf("input department:");
scanf("%s",record[i].department);
fwrite(&record[i],sizeof(struct rec),1,fp1);
}
fclose(fp1);
if((fp1=fopen("f:\\infile.txt","rb+"))==NULL)
{
printf("cannot open file");
exit(1);
}
printf("****************************************************\n");
printf("%-10s%-15s%-15s\n","id","name","department");
printf("****************************************************\n");
for(i=0;i<NUM;i++)
{
fread(&record[i],sizeof(struct rec),1,fp1);
printf("%-10s%-15s%-15s\n",record[i].id,record[i].name,record[i].department);
}
fseek(fp1,2*sizeof(struct rec),0);
fwrite(&record[1],sizeof(struct rec),1,fp1);
rewind(fp1);
printf("**********************************************************\n");
printf("%-10s%-15s%-10s\n","id","name","department");
printf("**********************************************************\n");
for(i=0;i<NUM;i++)
{
fread(&record[i],sizeof(struct rec),1,fp1);
printf("%-10s%-15s%-15s\n",record[i].id,record[i].name,record[i].department);
}
fclose(fp1);
scanf("%d",i);
}