| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1770 人关注过本帖
标题:程序运行到这个函数会奔溃,有什么错吗。。
只看楼主 加入收藏
惧惧
Rank: 1
等 级:新手上路
帖 子:93
专家分:0
注 册:2016-6-19
结帖率:90.91%
收藏
已结贴  问题点数:10 回复次数:16 
程序运行到这个函数会奔溃,有什么错吗。。
函数的代码在这里了
程序代码:
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
linlulu001
Rank: 13Rank: 13Rank: 13Rank: 13
等 级:贵宾
威 望:20
帖 子:944
专家分:4047
注 册:2016-4-13
收藏
得分:8 
这几天看到几个人用fread()函数。感觉fread()在读取结构体的时候很方便,代码只要简单的一句就将文件里的信息就读取出来。
殊不知fread根本不适合与结构体联合使用,fread适合读取基本类型的数据。
struct node
{
    char a[10];
    int a;
    char c[4];
}
以上面这个结构体为例
首先,它代码中是直接读取结构体大小的数据块,但无法自动将数据块中的字符序列或整数对应到结构体中的每个成员。
其次,结构体中字符数组的大小设置好了,所以fread读入数据块中,前10个字节赋给了a,第11到第14个字节赋给了b,以此类推。。。
最后结果可想而知道(这里卖个小关子,不理解的看客用sizeof测试struct node的大小,是18,还是20,如果错了再重温下结构体的知识)。

因为fread读入数据和赋值特性,注定字符串最后一个字符‘\0’是没办添加的(如果有回车没关系,能自动转换‘\0’)。
说的已经够多了,想要进一步了解fread自己去查相关资料。

楼主代码崩溃应该是字节计算失误造成的。
哪怕计算对,输出的也是一堆乱码。

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

2016-09-04 08:47
ehszt
Rank: 12Rank: 12Rank: 12
等 级:贵宾
威 望:40
帖 子:1745
专家分:3216
注 册:2015-12-2
收藏
得分:0 
我觉得是这句造成的
    while(fread(&fl[n],sizeof(struct flyline),1,fi))/*文件中有几个学生信息结构*/
     n++;
fread的返回值是一个不断增加的值,成功读取多少次返回多少。这样就造成了一个死循环,n无限增加,导致崩溃
linlulu001版主说的不能用fread读取结构体一说我不赞同。只要用的是同一个结构体定义的结构体变量,就可以读取和写入文件。
我曾经编过一个程序,里面的数据项就是用结构体写入文件的,读取写入很多次也没出问题。不过读取时要省略后面小于结构体大小的n个字节,具体怎么处理,各人有各人的方法。
2016-09-04 09:21
惧惧
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
ehszt
Rank: 12Rank: 12Rank: 12
等 级:贵宾
威 望:40
帖 子:1745
专家分:3216
注 册:2015-12-2
收藏
得分:0 
记得以前帮你改过,
  while(!feof(fi))
{
   fread(&fl[n],sizeof(struct flyline),1,fi);
      n++;
}
2016-09-04 09:28
linlulu001
Rank: 13Rank: 13Rank: 13Rank: 13
等 级:贵宾
威 望:20
帖 子:944
专家分:4047
注 册:2016-4-13
收藏
得分:0 
回复 3楼 ehszt
我可没说不能,只是说不合适。请你搞清楚
2016-09-04 10:40
惧惧
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
linlulu001
Rank: 13Rank: 13Rank: 13Rank: 13
等 级:贵宾
威 望:20
帖 子:944
专家分:4047
注 册:2016-4-13
收藏
得分:0 
想让别人帮你修改,麻烦文本文件发出来。
不想改了半天,最后发现是文本文件的问题。
2016-09-04 12:05
ehszt
Rank: 12Rank: 12Rank: 12
等 级:贵宾
威 望:40
帖 子:1745
专家分:3216
注 册:2015-12-2
收藏
得分:2 
试试这个。
反正我试了是没问题的,就是格式有点问题,一行容不了那么多
#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[10];
     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.txt","w"))==NULL)
     {
         printf("文件读取失败……");
         
     }
     for(i=0;i<17;i++)
     fwrite(&line[i],sizeof(struct flyline),1,fline);
     n=i;
      
     fclose(fline);

   
         
         
         
     fi=fopen("finformation.txt","r");
     if(fi==NULL)
     {
         printf("文件打开失败,请重新打开... ...\n");
         exit(1);
     }
     i=0;
     while(!feof(fi))
 {
    fread(&fl[i],sizeof(struct flyline),1,fi);
    i++;
 }
do{
     printf("请输入航班号(如AB1234):\n");
     scanf("%s",num4);
     for(i=0;i<n;i++)
     {
         
         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);
             break;
         }
         else
             p4=1;
         
     }
     if(p4==1)
     printf("重新输入:\n");

 }while(num4[0]!='q');
     
     
     fclose(fi);
     return 0;
 }

2016-09-04 12:12
快速回复:程序运行到这个函数会奔溃,有什么错吗。。
数据加载中...
 
   



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

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