您的位置:首页 > 其它

定义一个结构体:struct employee{int ID;char name[20];fl...

2012-11-26 22:09 323 查看

#include <stdio.h>
#include <stdlib.h>

typedef struct
{
    int ID;
    char name[20];
    float salary;
}employee;

int main()
{
    employee e[3] = {{1,"张三",3500},{2,"李四",3600},
     {3,"王麻子",3800}};
 FILE *fp = NULL;
//"r+" 读和写(文件的头部开始) "w+" 读写(覆盖) "a+" 读写(追加)
 if((fp=fopen("d.txt","w+"))==NULL)
 {
  printf("文件打开失败\n");
  return 0;
 }
 printf("文件打开成功\n");
 int count = 0;
//fread,fwrite 返回值:成功进行了多少次读写
 if((count=fwrite(e,sizeof(employee),3,fp))<3)
 {
  printf("写入失败\n");
 }
 fseek(fp,-3*sizeof(employee),SEEK_CUR);//rewind
 employee temp;
 int i = 0;
 for(;i<3;i++)
 {
  fread(&temp,sizeof(employee),1,fp);
  printf("工号:%d 姓名:%s 薪资:%g\n",temp.ID,temp.name,
        temp.salary);
 }
 fclose(fp);
    system("PAUSE");
    return 0;
}

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐