| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1771 人关注过本帖
标题:程序运行到这个函数会奔溃,有什么错吗。。
取消只看楼主 加入收藏
惧惧
Rank: 1
等 级:新手上路
帖 子:93
专家分:0
注 册:2016-6-19
结帖率:90.91%
收藏
已结贴  问题点数:10 回复次数:7 
程序运行到这个函数会奔溃,有什么错吗。。
函数的代码在这里了
程序代码:
void findnum(char num[])
{
    int n=0,i;
    FILE *fi;
    fi=fopen("finformation.dat","rb");
    if(fi==0) 
    {
        printf("文件打开失败,请重新打开... ...\n");
        exit(1);
    }
    while(fread(&fl[n],sizeof(struct flyline),1,fi))/*文件中有几个学生信息结构*/
    n++;
    
    for(i=0;i<n;i++)
    {
        fread(&fl[i],sizeof(struct flyline),1,fi);
        if(strcmp(fl[i].fnum,num)==0)
        {
            z4=i; 
            printf("您所查询的航班为:\n");
            printf("  %-8s%-8s%-8s%-10s%-14s%-8s%-16s%-12s%-16s\n","航班号","出发地","降落地","起飞时间","预计飞行时间","头等舱票价","头等舱剩余座位","经济舱票价","经济舱剩余座位");
            printf("  %-8s%-8s%-8s%-10s%-14s%-8s%-16s%-12s%-16s\n",fl[i].fnum,fl[i].fstart,fl[i].fend,fl[i].fstime,fl[i].ftime,fl[i].ftprice,fl[i].ftrest,fl[i].fyprice,fl[i].fyrest); 
        }
        
    }
    fclose(fi);
} 
2016-09-03 23:04
惧惧
Rank: 1
等 级:新手上路
帖 子:93
专家分:0
注 册:2016-6-19
收藏
得分:0 
回复 2楼 linlulu001
那这个部分可以怎么修改一下吗?或者用其他的函数的话,该用哪个什么函数啊?谢谢~
2016-09-04 09:23
惧惧
Rank: 1
等 级:新手上路
帖 子:93
专家分:0
注 册:2016-6-19
收藏
得分:0 
回复 3楼 ehszt
那您觉得我那句应该怎么改一下不会奔溃啊。。
2016-09-04 09:24
惧惧
Rank: 1
等 级:新手上路
帖 子:93
专家分:0
注 册:2016-6-19
收藏
得分:0 
回复 7楼 linlulu001
这是整个函数加上了前面的部分,还是奔溃,请帮我看一下需要改哪里,谢谢~
程序代码:
#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 fl[81];
int main(int argc, char *argv[])
{
    char num4[6];
    int n=0,i,z4;
    FILE *fi;
     
    int h4,c4;
    struct flyline line[31]={{"航班号","所属公司","出发地","降落地","起飞时间","预计飞行时间","机型","准点率","头等舱票价","头等舱剩余座位","经济舱票价","经济舱剩余座位","驾驶员","副驾驶\n"},
                             {"SC8835","山东航空公司","北京","珠海","7","210min","738","93%","5080","6","2030","33","Zhao Yi","Zhao Er\n"},
                             {"SC1195","山东航空公司","北京","银川","7","135min","738","98%","3540","8","1180","25","zhao san","zhao si\n"},  
                              {"SC4785","山东航空公司","北京","盐城","8","115min","738","80%","1960","2","980","12","zhao wu","zhao liu\n"},
                              {"SC4852","山东航空公司","北京","青岛","10","90min","747","90%","1640","5","820","34" ,"zhao qi","zhao ba\n"},
                              {"SC4882","山东航空公司","北京","珠海","11","240min","635","96%","5240","2","2320","11","zhao jiu","zhao shi\n"},
                             {"SC4854","山东航空公司","北京","烟台","14","100min","482","92%","4200","0","2100","9","qian yi","qian er\n"},
                             {"SC4678","山东航空公司","北京","厦门","15","100min","482","95%","2440","5","1220","17","qian san","qian si\n"},
                             {"SC4856","山东航空公司","北京","重庆","20","185min","738","85%","3860","1","1980","3","qian wu","qian liu\n"},
                             {"3U8548","四川航空公司","成都","北京","6","190min","321","78%","4730","3","1690","28","qian qi","qian ba\n"},
                             {"3U8303","四川航空公司","哈尔滨","北京","7","120min","319","81%","3050","7","1520","13","qian jiu","qian shi\n"},
                             {"3U8896","四川航空公司","西昌","北京","7","305min","320","91%","4820","3","2410","3","sun yi","sun er\n"},
                             {"3U8838","四川航空公司","昆明","北京","8","295min","320","87%","3460","0","1730","23","sun san","sun si\n"},
                             {"3U8832","四川航空公司","万县","北京","17","140min","319","86%","2850","4","1920","5","sun wu","sun liu\n"},
                             {"3U8894","四川航空公司","成都","北京","19","190min","320","90%","4730","1","1690","0","sun qi","sun ba\n"},
                             {"3U8830","四川航空公司","重庆","北京","10","195min","321","87%","3660","4","1820","31","sun jiu","sun shi\n"},
                             {"3U8892","四川航空公司","三亚","北京","7","245min","321","93%","3870","2","1980","19","li san","li si\n"},
                              
                            };
                            
    int d1,p4;
    FILE*fline;
    if((fline=fopen("finformation.dat","ab+"))==NULL)
    {
        printf("文件读取失败……");
        
    }
    for(d1=0;d1<31;d1++)
    if(fwrite(&line[d1],sizeof(struct flyline),1,fline)!=1)
    printf("信息未能加载……");
     
    fclose(fline);

   
        printf("请输入航班号(如AB1234):\n");
        scanf("%s",num4);
        
        
    fi=fopen("finformation.dat","rb");
    if(fi==NULL) 
    {
        printf("文件打开失败,请重新打开... ...\n");
        exit(1);
    }
    while(!feof(fi))
{
   fread(&fl[n],sizeof(struct flyline),1,fi);
      n++;
}
    
do{
    scanf("%s",num4);
    for(i=0;i<n;i++)
    {
        fread(&fl[i],sizeof(struct flyline),1,fi);
        if((strcmp(fl[i].fnum,num4))==0)
        {
            z4=i;
            p4=0; 
            printf("您所查询的航班为:\n");
            printf("  %-8s%-8s%-8s%-10s%-14s%-8s%-16s%-12s%-16s\n","航班号","出发地","降落地","起飞时间","预计飞行时间","头等舱票价","头等舱剩余座位","经济舱票价","经济舱剩余座位");
            printf("  %-8s%-8s%-8s%-10s%-14s%-8s%-16s%-12s%-16s\n",fl[i].fnum,fl[i].fstart,fl[i].fend,fl[i].fstime,fl[i].ftime,fl[i].ftprice,fl[i].ftrest,fl[i].fyprice,fl[i].fyrest); 
        }
        else
            p4=1;
        
    }
    if(p4==1)
    printf("重新输入:"); 
    
}while(p4==0);
    
    
    fclose(fi);
    return 0;
}
2016-09-04 10:48
惧惧
Rank: 1
等 级:新手上路
帖 子:93
专家分:0
注 册:2016-6-19
收藏
得分:0 
回复 10楼 ehszt
啊谢谢~~但是当我输对了的时候还让我再输航班号,我想让他在我输对的时候进入下一个叫做 mai_piao()的函数该怎么办?而且我的文件是二进制的是.dat的格式,现在都是.txt 不能这样改啊!

[此贴子已经被作者于2016-9-4 12:28编辑过]

2016-09-04 12:21
惧惧
Rank: 1
等 级:新手上路
帖 子:93
专家分:0
注 册:2016-6-19
收藏
得分:0 
回复 9楼 linlulu001
文本文件是这个吗
finformation.rar (2.44 KB)
2016-09-04 12:30
惧惧
Rank: 1
等 级:新手上路
帖 子:93
专家分:0
注 册:2016-6-19
收藏
得分:0 
回复 12楼 ehszt
那个程序单独可以运行了,但是一放到我的大的程序里 当做一个函数的时候,就又崩溃了,怎么办啊
2016-09-04 13:31
惧惧
Rank: 1
等 级:新手上路
帖 子:93
专家分:0
注 册:2016-6-19
收藏
得分:0 
回复 16楼 linlulu001
我写的全部代码在这里,里面还有一部分是错的,我还没改到那几个部分,谢谢!
Desktop.rar (5.26 KB)
2016-09-04 15:01
快速回复:程序运行到这个函数会奔溃,有什么错吗。。
数据加载中...
 
   



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

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