不厭徐何 2023-04-02 19:14 采纳率: 56.5%
浏览 32
已结题

文件读入输出-c语言

输入职工信息,建立一个磁盘文件,再将职工名,工资另建一个简明的职工工资文件。
不知道哪里错了


#include <stdio.h>
#include <stdlib.h>
struct employee{
    char name[20];
    int num;
    char sex;
    int age;
    char addr[20];
    float salary;
    char health[8];
    char education[10];
}person[2];
 int main(){
     //建立磁盘文件
     FILE *fp;
     if((fp=fopen("employee.dat","wb"))==NULL){
         printf("cannot open file\n");
         exit(0); 
     } 
     printf("enter :\n");
     int i;
     for(i=0;i<2;i++)
     scanf("%s%d%c%d%s%f%s%s",person[i].name,&person[i].num,&person[i].sex,&person[i].age,
     person[i].addr,&person[i].salary,person[i].health,person[i].education);
     //写入磁盘
     for(i=0;i<2;i++)
     fwrite(&person[i],sizeof(struct employee),1,fp);
     fclose(fp); //完毕
     
     struct employee person1[2];
     struct employee1{
         char name[20];
         float salary;
     }person2[2]; 
     //读出磁盘文件
     if((fp=fopen("employee.dat","rb"))==NULL){
         printf("cannot open file\n");
         exit(0);
     } 
     for(i=0;i<2;i++){
     fread(&person1[i],sizeof(struct employee),1,fp);
     strcpy(person2[i].name,person1[i].name);
     person2[i].salary=person1[i].salary;
 }
     fclose(fp);
     FILE *fp1;
     if((fp1=fopen("employee1.dat","wb"))==NULL){
         printf("cannot open file\n");
         exit(0);
     } 
     for(i=0;i<2;i++)
     fwrite(&person2[i],sizeof(struct employee1),1,fp);
     fclose(fp1);
     return 0;
 }
  • 写回答

2条回答 默认 最新

  • a5156520 2023-04-02 20:12
    关注

    获取信息那里改一下,在%c之前增加一个空格,忽略输入中的空格;

    然后增加打印文件信息的代码,以便检查输入的信息是否正确即可。

    修改如下:

    参考链接:

    
    
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h> 
    struct employee{
        char name[20];
        int num;
        char sex;
        int age;
        char addr[20];
        float salary;
        char health[8];
        char education[10];
    }person[2];
     int main(){
         //建立磁盘文件
         FILE *fp;
         if((fp=fopen("employee.dat","wb"))==NULL){
             printf("cannot open file\n");
             exit(0); 
         } 
         printf("enter :\n");
         int i;
         for(i=0;i<2;i++)
         // https://blog.csdn.net/weixin_45729253/article/details/103095937
         // %c前面增加一个空格,以便忽略输入的空格 
         scanf("%s%d %c%d%s%f%s%s",person[i].name,&person[i].num,&person[i].sex,&person[i].age,
         person[i].addr,&person[i].salary,person[i].health,person[i].education);
         //写入磁盘
         for(i=0;i<2;i++)
         fwrite(&person[i],sizeof(struct employee),1,fp);
         fclose(fp); //完毕
         
         struct employee person1[2];
         struct employee1{
             char name[20];
             float salary;
         }person2[2]; 
         //读出磁盘文件
         if((fp=fopen("employee.dat","rb"))==NULL){
             printf("cannot open file\n");
             exit(0);
         } 
         
         for(i=0;i<2;i++){
             fread(&person1[i],sizeof(struct employee),1,fp);
             // 增加打印文件信息的代码,以检查数据是否输入正确 
             printf("%s %d %c %d %s %f %s %s\n",person[i].name,person[i].num,person[i].sex,person[i].age,
             person[i].addr,person[i].salary,person[i].health,person[i].education);
             strcpy(person2[i].name,person1[i].name);
             person2[i].salary=person1[i].salary;
         } 
         fclose(fp);
         
         FILE *fp1;
         if((fp1=fopen("employee1.dat","wb"))==NULL){
             printf("cannot open file\n");
             exit(0);
         } 
         for(i=0;i<2;i++)
             fwrite(&person2[i],sizeof(struct employee1),1,fp);
         fclose(fp1);
         
         if((fp1=fopen("employee1.dat","rb"))==NULL){
             printf("cannot open file\n");
             exit(0);
         } 
        struct employee1 personRead[2];
         for(i=0;i<2;i++){
             fread(&personRead[i],sizeof(struct employee1),1,fp);
               // 增加打印文件信息的代码,以检查数据是否输入正确 
             printf("%s %f\n",personRead[i].name,personRead[i].salary);
         }
             
         fclose(fp1);
         
         return 0;
     }
    
    
    

    img

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论 编辑记录
查看更多回答(1条)

报告相同问题?

问题事件

  • 系统已结题 4月10日
  • 已采纳回答 4月2日
  • 创建了问题 4月2日

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度