定义一个结构体: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;
}
相关文章推荐
- .使用面向对象思想,输出员工信息并计算员工的工资。实现过程为:定义一个部门(Department)类,该类有部门编号(Id)、部门名称(Name)属性; 再定义一个员工(Employee)类,该类的属
- Id(int) name(text) 1 网友 2 网友 3 网友 4 网友 5 网友 请写出一个SQL语句,把name字段更新成name+id 也就
- 定义一个笔记本类,该类有颜色(char)和cpu型号(int)两个属性。 [必做题] 3.1 无参和有参的两个构造方法;有参构造方法可以在创建对象的同时为每个属性赋值; 3.2 输出笔记本信息的方法
- filenode* getfilesname(char *dir, int *pno) { DIR *dir_prt; struct dirent *entry; struct fi
- 我似乎不能成功定义一个链表。我试过 typedef struct { char *item; NODEPTR next; } *NODEPTR; 但是编译器报了错误信息。难道在C语言中一个结构不能包
- 定义一个学生结构体类型struct student,里边含有学号,姓名,分数,定义结构体数组struct student s[3],给数组赋初值后, 写入文件,然后通过lseek偏移到开头,然后再读取
- static inline int register_chrdev(unsigned int major, const char *name, const struct file_operations
- 【程序员面试宝典】【疑问】6.1宏定义 用一个宏定义FIND求一个结构体struct里某个变量相对struc的编移量
- html中 target 的所有 4 个值都以下划线开始。任何其他用一个下划线作为开头的窗口或者目标都会被浏览器忽略,因此,不要将下划线作为文档中定义的任何框架 name 或 id 的第一个字符。
- 结构体最后定义一个char p[0];这样的成员有何意义(转)
- variantt转int:char转换Variant?怎么转定义一个charx[]
- OC中定义了一个结构体(struct),设置结构体的值
- Android中通过反射获取资源Id(特别用在自己定义一个工具将其打成.jar包时,特别注意资源的获取)
- 定义一个Employee类并排序(完整版本)
- 请编写一个c程序确定signed,unsigned的char,short,int和long变量取值范围
- [置顶] C语言,从字符串中提取一个字符串,int substr(char dst[], char src[],int start,int len)目标是:从src数组的起始位置开始向后偏移star
- 习题 9.1 定义一个结构体变量(包括年、月、日)。计算该日在本年中是第几天,注意闰年问题。
- 结构体定义 typedef struct 用法详解和用法小结
- 结构体定义:struct与typedef struct 用法详解和用法小结
- 结构体定义 typedef struct 用法详解和用法小结