TA的每日心情 | 开心 2014-6-18 08:29 |
---|
签到天数: 14 天 [LV.3]偶尔看看II
滴水大师
 
- 积分
- 2345
|
题目
解决代码及点评
- <pre code_snippet_id="91880" snippet_file_name="blog_20131202_1_2646179" class="cpp" name="code"></pre><pre code_snippet_id="91880" snippet_file_name="blog_20131202_1_2646179" class="cpp" name="code"><pre code_snippet_id="91880" snippet_file_name="blog_20131202_1_2646179" class="cpp" name="code"><pre code_snippet_id="91880" snippet_file_name="blog_20131202_1_2646179" class="cpp" name="code">/*
- 3. 将10名职工的数据从键盘输入, 然后送入磁盘文件worker1. rec 中保存。
- 设职工数据包括:职工号、职工名、性别、年龄、工资,再从磁盘调入这些数据,
- 依次打印出来(用 fread和fwrite函数)。
- */
- #include <stdio.h>
- #include <stdlib.h>
- typedef struct Staff_9_3
- {
- int num;
- char name[30];
- char sex[5];
- int age;
- float salary;
-
- } Employee;
-
- /*
- 保存员工信息
- */
- void saveInfo93(Employee * emp,int n)
- {
- FILE *fp = NULL;
- fopen_s(&fp, "worker1.rec","wb");
- if (fp)
- {
- for (int i = 0; i < n; i++)
- {
- fwrite(&emp,sizeof(emp),1,fp);
- }
- fclose(fp);
- }
- else
- {
- printf("open file failed!");
- }
- }
- /*
- 读取员工信息
- */
- void readInfo93()
- {
- FILE *fp = NULL;
- Employee tempEmp;
- fopen_s(&fp, "worker1.rec","rb");
- if (fp)
- {
- fread(&tempEmp,sizeof(tempEmp),1,fp);
- while(!feof(fp))
- {
- printf("num = %d,name = %s,sex = %s,age = %d,salary = %f\n",
- tempEmp.num,tempEmp.name,tempEmp.sex,tempEmp.age,tempEmp.salary);
- fread(&tempEmp,sizeof(tempEmp),1,fp);
- }
- fclose(fp);
- }
- else
- {
- printf("open file failed!");
- }
- }
- void main()
- {
- const int N = 10;
- Employee emp[N];
- for (int i = 0; i < N; i++)
- {
- printf("请输入第%d员工的姓名:\n",(i + 1));
- scanf_s("%s",&(emp.name));
- printf("请输入第%d员工的性别:\n",(i + 1));
- scanf_s("%s",&(emp.sex));
- printf("请输入第%d员工的编号:\n",(i + 1));
- scanf_s("%d",&(emp.num));
- printf("请输入第%d员工的年龄:\n",(i + 1));
- scanf_s("%d",&(emp.age));
- printf("请输入第%d员工的工资:\n",(i + 1));
- scanf_s("%f",&(emp.salary));
- }
- saveInfo93(emp,N);
- readInfo93();
- system("pause");
- }</pre><br><br></pre></pre>
|
|