Seemyeyes7 2019-06-15 09:52 采纳率: 50%
浏览 857
已采纳

C语言用其中一个结构的体元素从文件中查找到相同的结构体之后如何从文件中输出查找到的那个结构体

功能要求:
① 职工基本信息的录入、增加、删除、修改;
② 按各种条件查询职工信息;
③ 应发工资计算;
④ 其它功能,如工资分布情况统计,代交税金计算,津贴发放,工资晋级等。









#include<stdio.h>
#include<string.h>
#include<stdlib.h>
int n;
void member_putin_info();
void member_seaech_info(struct member member[100],int n);
void member_add_info();
void member_modify_info();
void member_delete_info();
char Readspecailline_info(int i);

    struct member{
    char name[20];
    char post[20];
    int age,wages;
    int WorkingHours;
    };

    void member_putin_info(){
    int i;
    FILE *fp1,*fp2;
    struct member member[100];
    printf("Input n:");
    scanf("%d",&n);


    if((fp2=fopen("D:\\VC data\\salary1.txt","w"))==NULL)
    {printf("File open error!\n");
    exit(0);                                               
    }
    fprintf(fp2,"%d",n);

    if(fclose(fp2)){                           
    printf("can not close the file!\n");
    exit(0);
    }

    if((fp1=fopen("D:\\VC data\\salary.txt","w"))==NULL)
    {printf("File open error!\n");
    exit(0);                                              //*打开文件*//
    }

    for(i=0;i<n;i++){                                //*职工信息录入功能*//
    printf("Input name:\n");
    scanf("%s",&member[i].name);
    fprintf(fp1,"%s ",member[i].name);
    printf("Input post:\n");
    scanf("%s",&member[i].post);
    fprintf(fp1,"%s ",member[i].post);
    printf("Input age:\n");
    scanf("%d",&member[i].age);
    fprintf(fp1,"%d ",member[i].age);
    printf("Input wages:\n");
    scanf("%d",&member[i].wages);
    fprintf(fp1,"%d ",member[i].wages);
    printf("Input WorkingHours:\n");
    scanf("%d",&member[i].WorkingHours);
    fprintf(fp1,"%d ",member[i].WorkingHours);
    printf("\n");
    fprintf(fp1,"\n");

    }
    if(fclose(fp1)){                          //*关闭文件*//
    printf("can not close the file!\n");
    exit(0);
    }
    }



    void member_seaech_info()                                //*职工信息查询功能*// 
    {
       struct member member[100];
        FILE *fp1,*fp2,*fp3;
if((fp2=fopen("D:\\Vc data\\salary.txt","r"))==NULL)
    {printf("File open error!\n");
exit(0);  }   
   fscanf(fp2,"%d",&n);


if((fp1=fopen("D:\\Vc data\\salary.txt","r"))==NULL)
    {printf("File open error!\n");
exit(0);  }          
     int i;
    char Sname[20]; 
    char Spost[20];
     int choice; 
     int age; 
     char ch[2];

     printf("[1]Select name\n");
     printf("[2]Select post\n");
     printf("[3]Select age\n");
     printf("[4]Selcet wages\n");
     printf("[5]Select WorkingHours\n");

     printf("Enter choice:");
     scanf("%d",&choice);
     if(choice<1||choice>5)
         printf("输入错误!\n");

     if(choice==1){
printf("Enter the name:");
scanf("%s",&Sname);
for(i=0;i++;i<n){
    if(strcmp(Sname,member[i].name)==0){
    break;
    }
}
     }



if(fclose(fp1)){                         
    printf("can not close the file!\n");
    exit(0);
    }

if(fclose(fp2)){                          
    printf("can not close the file!\n");
    exit(0);}
}



















    int main(void)
    { int choice;
    printf("enter your choice:");
    scanf("%d",&choice);    
    switch(choice){
        case 1:member_putin_info();break;
        case 2:member_seaech_info();break;

    }

return 0;
}

     我就用名字进行对比,完了之后找到第几个结构有相同的名字,想输出这个结构体的所有元素,但是用scanf(fp1,"%s",member[i].name)这样的做法貌似永远只取值我文件中的第一行,求大神教教我方法!!
只要方法能行可靠就okk!!
  • 写回答

3条回答 默认 最新

  • threenewbee 2019-06-15 10:33
    关注
    没有你txt文件的内容和member的定义,没法给你调试和编写代码
    
    只能大概说说
    
    struct member member[100];
    
    你定义了member[100]
    但是没有读取记录放入member里面的逻辑
    在
    if((fp1=fopen("D:\Vc data\salary.txt","r"))==NULL)
    {printf("File open error!\n");
    exit(0); }
    下面
    for (int i = 0; i < n; i++)
    {
    fscanf(fp, "%s %d,...", member[i].sname, &member[i].age); //这一行是大概的演示,要根据你结构体的内容和文件的内容去修改
    }
    ...
    然后
    for(i=0;i++;i<n){
    if(strcmp(Sname,member[i].name)==0){
    break;
    }
    }
    printf("%s", member[i].sname); //此时i就是找到的数据的下标
    这样就能查询了
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 如何用stata画出文献中常见的安慰剂检验图
  • ¥15 c语言链表结构体数据插入
  • ¥40 使用MATLAB解答线性代数问题
  • ¥15 COCOS的问题COCOS的问题
  • ¥15 FPGA-SRIO初始化失败
  • ¥15 MapReduce实现倒排索引失败
  • ¥15 ZABBIX6.0L连接数据库报错,如何解决?(操作系统-centos)
  • ¥15 找一位技术过硬的游戏pj程序员
  • ¥15 matlab生成电测深三层曲线模型代码
  • ¥50 随机森林与房贷信用风险模型