| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1510 人关注过本帖
标题:疑问求解答~ 谢谢!结构数组的初始化是否存在对程序造成了影响
只看楼主 加入收藏
是心愿
Rank: 1
等 级:新手上路
帖 子:75
专家分:0
注 册:2016-6-22
结帖率:62.5%
收藏
已结贴  问题点数:20 回复次数:5 
疑问求解答~ 谢谢!结构数组的初始化是否存在对程序造成了影响
请问,我先运行一次单个的,在这个程序之外的结构体文件,再运行一下下面的程序,就可以识别一样的航班,如果航班重复的话会让我重新输入。
但是,如果我去掉struct flyline line【18】等号后边的一大串,为什么就不能识别相同航班了呢?也就是不论我输入什么航班都会顺利通过,正常来讲,有文件存在的情况下,不论我去不去掉那一大串,对程序来说是没有影响的啊


#include <stdio.h>
#include<stdlib.h>
#include<string.h>
#define whole 81

struct flyline
    {
        char fnum[whole];
        char fcompany[whole];
        char fstart[whole];
        char fend[whole];
        char fstime[whole];
        char ftime[whole];
        char ftype[whole];
        char frate[whole];
        char ftprice[whole];
        char ftrest[whole];
        char fyprice[whole];
        char fyrest[whole];
        char fp1[whole];
        char fp2[whole];
    };
   
    struct flyline line[18]={{"航班号","所属公司","出发地","降落地","起飞时间","预计飞行时间","机型","准点率","头等舱票价","头等舱剩余座位","经济舱票价","经济舱剩余座位","驾驶员","副驾驶\n"},
                             {"SC8835","山东航空公司","北京","珠海","6:35","210min","738","93%","¥5080","6","¥2030","33","Zhao Yi","Zhao Er\n"},
                             {"SC1195","山东航空公司","北京","银川","7:05","135min","738","98%","¥3540","8","¥1180","25","zhao san","zhao si\n"},  
                              {"SC4785","山东航空公司","北京","盐城","8:00","115min","738","80%","¥1960","2","¥980","12","zhao wu","zhao liu\n"},
                              {"SC4852","山东航空公司","北京","青岛","9:50","90min","747","90%","¥1640","5","¥820","34" ,"zhao qi","zhao ba\n"},
                              {"SC4882","山东航空公司","北京","珠海","10:45","240min","635","96%","¥5240","2","¥2320","11","zhao jiu","zhao shi\n"},
                             {"SC4854","山东航空公司","北京","烟台","13:45","100min","482","92%","¥4200","0","¥2100","9","qian yi","qian er\n"},
                             {"SC4678","山东航空公司","北京","厦门","15:20","100min","482","95%","¥2440","5","¥1220","17","qian san","qian si\n"},
                             {"SC4856","山东航空公司","北京","重庆","20:05","185min","738","85%","¥3860","1","¥1980","3","qian wu","qian liu\n"},
                             {"3U8548","四川航空公司","成都","北京","6:25","190min","321","78%","¥4730","3","¥1690","28","qian qi","qian ba\n"},
                             {"3U8303","四川航空公司","哈尔滨","北京","6:50","120min","319","81%","¥3050","7","¥1520","13","qian jiu","qian shi\n"},
                             {"3U8896","四川航空公司","西昌","北京","7:25","305min","320","91%","¥4820","3","¥2410","3","sun yi","sun er\n"},
                             {"3U8838","四川航空公司","昆明","北京","8:10","295min","320","87%","¥3460","0","¥1730","23","sun san","sun si\n"},
                             {"3U8832","四川航空公司","万县","北京","17:30","140min","319","86%","¥2850","4","¥1920","5","sun wu","sun liu\n"},
                             {"3U8894","四川航空公司","成都","北京","19:30","190min","320","90%","¥4730","1","¥1690","0","sun qi","sun ba\n"},
                             {"3U8830","四川航空公司","重庆","北京","10:45","195min","321","87%","¥3660","4","¥1820","31","sun jiu","sun shi\n"},
                             {"3U8892","四川航空公司","三亚","北京","7:55","245min","321","93%","¥3870","2","¥1980","19","li san","li si\n"},
                              
                            };
         struct flyline finformation;
         struct flyline s[81];
                           

int main()
{
         
        int i,p3,t3,q3;

        do /*航班信息输入*/
        {  
               
               p3=1;
            q3=1;
            t3=1;
            printf("请输入一下您要更改的信息:\n");
            printf("请输入新增航班号: ");
            scanf("%s",&finformation.fnum);
            printf("请输入所属公司: ");
            scanf("%s",&finformation.fcompany);
            printf("请输入出发地: ");
            scanf("%s",&finformation.fstart);
            printf("请输入降落地: ");
            scanf("%s",&finformation.fend);
            printf("请输入起飞时间: ");
            scanf("%s",&finformation.fstime);
            printf("请输入预计飞行时间: ");
            scanf("%s",&finformation.ftime);
            printf("请输入机型: ");
            scanf("%s",&finformation.ftype);
            printf("请输入准点率: ");
            scanf("%s",&finformation.frate);
            printf("请输入头等舱票价: ");
            scanf("%s",&finformation.ftprice);
            printf("请输入头等舱剩余座位: ");
            scanf("%s",&finformation.ftrest);
            printf("请输入经济舱票价: ");
            scanf("%s",&finformation.fyprice);
            printf("请输入经济舱剩余座位: ");
            scanf("%s",&finformation.fyrest);
            printf("请输入驾驶员: ");
            getchar();/*接收回车符*/
            gets(finformation.fp1);
            printf("请输入副驾驶: ");
            gets(finformation.fp2);
           
              for(i=0;i<18;i++)
           {   
               
               if(strcmp(line[i].fnum,finformation.fnum)==0)  p3=0;
               if(strcmp(line[i].fp1,finformation.fp1)==0)  q3=0;
               if(strcmp(line[i].fp2,finformation.fp2)==0)  t3=0;
               
           }
        }while(p3==0||q3==0||t3==0);
        
        
      FILE *fline;
      int d1,n3_1=0;
      fline=fopen("finformation.txt","ab+");/*打开文件*/
      fseek(fline,0,SEEK_END);/*将新增航班信息加到结构末尾*/
       fwrite(&finformation,sizeof(struct flyline),1,fline);/*将输入信息写入文件*/
       fclose(fline);
       printf("成功添加航班信息!\n");
       fclose(fline);
      //struct flyline s[81];
  
       if((fline=fopen("finformation.txt","rb"))==NULL)
      {
        printf("文件读取失败……");
        exit(0);
         }
      
       while(!feof(fline))/*统计全部航班信息个数*/
         {
                     fread(&s[n3_1],sizeof(struct flyline),1,fline);
                    n3_1++;
         
         }
      
       for(d1=0;d1<n3_1;d1++)/*读取并输出全部航班信息*/
     {
        fread(&s[d1],sizeof(struct flyline),1,fline);
        printf("  %-8s%-14s%-8s%-8s%-10s%-14s%-7s%-8s%-12s%-16s%-12s%-16s%-10s%-6s\n",s[d1].fnum,s[d1].fcompany,s[d1].fstart,s[d1].fend,s[d1].fstime,s[d1].ftime,s[d1].ftype,s[d1].frate,s[d1].ftprice,s[d1].ftrest,s[d1].fyprice,s[d1].fyrest,s[d1].fp1,s[d1].fp2);
     }
         fclose(fline);
      
      
      return 0;      
}
搜索更多相关主题的帖子: include 结构体 whole 影响 
2016-09-05 16:31
ehszt
Rank: 12Rank: 12Rank: 12
等 级:贵宾
威 望:40
帖 子:1745
专家分:3216
注 册:2015-12-2
收藏
得分:20 
我只说道理,不修改。
删去了初始化的数据,那你至少应该从文件中读取数据到该数组。不然和什么比较。可能性比较大的就是你的读取出了问题。
如果是细节问题,那自己要好好把握。
2016-09-05 16:57
是心愿
Rank: 1
等 级:新手上路
帖 子:75
专家分:0
注 册:2016-6-22
收藏
得分:0 
回复 2楼 ehszt
您好,请问是不是在do循环之前加这个,但是加完之后就编译没有错,运行还是崩溃,请问问题出在哪?谢谢~
  if((fline=fopen("finformation.txt","rb"))==NULL)/*打开文件*/
      {
        printf("文件读取失败……");
    exit(0);
         }
         
         for(d1=0;d1<31;d1++)/*读取全部信息*/
    {
        fread(&line[d1],sizeof(struct flyline),1,fline);
        printf("  %-8s%-14s%-8s%-8s%-10s%-14s%-7s%-8s%-12s%-16s%-12s%-16s%-10s%-6s\n",line[d1].fnum,line[d1].fcompany,line[d1].fstart,line[d1].fend,line[d1].fstime,line[d1].ftime,line[d1].ftype,line[d1].frate,line[d1].ftprice,line[d1].ftrest,line[d1].fyprice,line[d1].fyrest,line[d1].fp1,line[d1].fp2);
    }
      fclose(fline);
2016-09-05 17:46
ehszt
Rank: 12Rank: 12Rank: 12
等 级:贵宾
威 望:40
帖 子:1745
专家分:3216
注 册:2015-12-2
收藏
得分:0 
for(d1=0;!feof(fline);d1++)/*读取全部信息*/
试试
2016-09-05 17:49
是心愿
Rank: 1
等 级:新手上路
帖 子:75
专家分:0
注 册:2016-6-22
收藏
得分:0 
回复 4楼 ehszt
您好,还是崩溃,在读出并显示所有航班信息后就崩溃了
2016-09-05 17:53
是心愿
Rank: 1
等 级:新手上路
帖 子:75
专家分:0
注 册:2016-6-22
收藏
得分:0 
回复 4楼 ehszt
找出来错误了 谢谢!
2016-09-05 18:38
快速回复:疑问求解答~ 谢谢!结构数组的初始化是否存在对程序造成了影响
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.030247 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved