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">/*
- 7. 删除worker2.rec中某个序号的职工记录,
- 再存入原文件中(用fread和fwrite函数)
- */
- #include <stdio.h>
- #include <stdlib.h>
-
- typedef struct Staff_9_7
- {
- int num;
- char name[30];
- char sex[5];
- int age;
- float salary;
-
- } Employee;
- /*
- 读取员工信息
- */
- void readStaInfo_7(Employee *tempEmp)
- {
- Employee temp;
- FILE *fp = NULL;
- fopen_s(&fp, "worker2.rec","rb");
- if (fp)
- {
- int i = 0;
- fread(&temp,sizeof(temp),1,fp);
- while(!feof(fp))
- {
- tempEmp = temp;
- printf("num = %d,name = %s,sex = %s,age = %d,salary = %f\n",
- tempEmp.num,tempEmp.name,tempEmp.sex,tempEmp.age,tempEmp.salary);
- fread(&temp,sizeof(temp),1,fp);
- tempEmp = temp;
- i++;
- }
- fclose(fp);
- }
- else
- {
- printf("open file failed!");
- }
- }
-
- /*
- 保存员工信息
- */
- void saveEmpInfo97(Employee * emp,int n)
- {
- FILE *fp = NULL;
- fopen_s(&fp, "worker2.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 main()
- {
- const int N = 11;
- int num;
- int index = 0;
- Employee emp[N];
- Employee newEmp[N - 1];
- readStaInfo_7(emp);
- printf("请输入要删除员工的编号:\n");
- scanf_s("%d",&num);
- for (int i = 0; i < N; i++)
- {
-
- if (num == emp.num)
- {
- continue;
- }
- else
- {
- newEmp[index++] = emp;
- }
- }
- saveEmpInfo97(newEmp,N - 1);
- readStaInfo_7(newEmp);
- system("pause");
- }</pre><br><br><br></pre></pre>
|
|