程设小组作业——计算机配件的进货/销售管理系统

switch_int_to_name.h 识别代码与名称转换函数

/////////////////////////////////
 /*
     识别代码与名称转换 
 */ 
 void switch_int_to_name(int type,int name_code,char name[]){
     FILE *fp;
     int id_code;
     switch(type){
         case 1:
             fp=fopen("../data/name.txt","r+");
             break;
         case 2:
             fp=fopen("../data/name.txt","r+");
             break;
         case 3:
             fp=fopen("../data/name.txt","r+");
             break;
         case 4:
             fp=fopen("../data/parts.txt","r+");
             break;
         case 5:
             fp=fopen("../data/models.txt","r+");
             break;
         case 6:
             fp=fopen("../data/gift.txt","r+");
     }
     do{
         fscanf(fp,"%s",name);
         fscanf(fp,"%d",&id_code);
     }while(id_code!=name_code&&feof(fp)==0);
     if(id_code!=name_code)//如果文件中不存在此名称,则创建此名称 
     {
         printf("code not found,please input a new name add to the record:");
         scanf("%s",name);
         fprintf(fp,"\n%s %d",name,id_code+1);
         fclose(fp);
         return;//返回新创建的识别代码 
     }
     fclose(fp);
     return;
 }

点赞

发表评论