#include<stdio.h>
#include<stdlib.h>
typedef struct
{
int ID;
char name[20];
float salary;
}employee;
int main()
{
employee e[3]={
{1001,"Lucy",4500},
{1002,"Jim",5000},
{1003,"James",4800}
};
FILE* fp=NULL;
if((fp=fopen("d.txt","w+"))==NULL)
{
perror("文件打开失败!"),exit(0);
}
printf("文件打开成功!");
int count=0;
if((count=fwrite(e,sizeof(e),3,fp))<3)
{
perror("写入失败\n");
}
fseek(fp,-3*sizeof(employee),SEEK_CUR);
employee t;
int i;
for(i=0;i<3;i++)
{
fread(&t,sizeof(employee),1,fp);
printf("%d,%s,%.3f\n",t.ID,t.name,t.salary);
}
fclose(fp);
return 0;
}
#include<stdlib.h>
typedef struct
{
int ID;
char name[20];
float salary;
}employee;
int main()
{
employee e[3]={
{1001,"Lucy",4500},
{1002,"Jim",5000},
{1003,"James",4800}
};
FILE* fp=NULL;
if((fp=fopen("d.txt","w+"))==NULL)
{
perror("文件打开失败!"),exit(0);
}
printf("文件打开成功!");
int count=0;
if((count=fwrite(e,sizeof(e),3,fp))<3)
{
perror("写入失败\n");
}
fseek(fp,-3*sizeof(employee),SEEK_CUR);
employee t;
int i;
for(i=0;i<3;i++)
{
fread(&t,sizeof(employee),1,fp);
printf("%d,%s,%.3f\n",t.ID,t.name,t.salary);
}
fclose(fp);
return 0;
}