免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 4964 | 回复: 6
打印 上一主题 下一主题

[C] fclose的时候出现段错误 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2015-11-13 11:41 |只看该作者 |倒序浏览
gateway_file.h
  1. #ifndef _GATEWAY_FILE_H_
  2. #define _GATEWAY_FILE_H_
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <string.h>

  6. typedef struct DeviceInfo
  7. {
  8.         char type[10];
  9.         int id;
  10. }DeviceInfo;
  11. FILE *Fopen(char *path);
  12. int ReadFile(FILE *fp);
  13. void Fprint(DeviceInfo *di,int len);
  14. void Fclose(FILE *fp);

  15. #endif
复制代码
gateway_file.c
  1. #include "gateway_file.h"

  2. #define USE_DEBUG
  3. #ifdef USE_DEBUG
  4. #define DEBUG_ERR(fmt, args...) printf("\033[46;31m[line:%d]\033[0m "#fmt"\r\n",__LINE__, ##args)
  5. #define DEBUG_INFO(fmt, args...) printf("\033[33m[line:%d]\033[0m "#fmt"\r\n",__LINE__, ##args)
  6. #else
  7. #define DEBUG_ERR(fmt, ...)
  8. #define DEBUG_INFO(fmt, ...)
  9. #endif

  10. DeviceInfo *record;
  11. FILE *Fopen(char *path)
  12. {
  13.         FILE *fp = fopen(path,"r");
  14.         if(fp == NULL)
  15.                 DEBUG_ERR(open file %s failed,path);
  16.         return fp;
  17. }

  18. int ReadFile(FILE *fp)
  19. {
  20.         long len;
  21.         int i = 0;
  22.         char *type=NULL;
  23.         char *id=NULL;
  24.         char buffer[128] = "";
  25.         fseek(fp,0,SEEK_END);
  26.         len = ftell(fp);
  27.         printf("length for ftell in Read = %ld\n",len);
  28.         fseek(fp,0,SEEK_SET);
  29.         record = (DeviceInfo*)malloc(len);
  30.         //memset(record,0,len+40);
  31.         while(fgets(buffer,sizeof(buffer),fp) != NULL)
  32.         {
  33.                 type = strtok(buffer," ");
  34.                 id = strtok(NULL," ");
  35.                 if(type != NULL && id != NULL)
  36.                 {
  37.                         printf("sizeof type = %d\n",strlen(type));
  38.                         printf("sizeof id = %d\n",strlen(id));
  39.                         strcpy(record[i].type,type);
  40.                         record[i].id = atoi(id);
  41.                         i++;
  42.                 }
  43.         }
  44.         return i;
  45. }

  46. void Fprint(DeviceInfo *di,int len)
  47. {
  48.         int i;
  49.         printf("length=%d\n",len);
  50.         for(i=0; i<len; i++)
  51.         {
  52.                 printf("record[%d].type=%s\t",i,di[i].type);
  53.                 printf("record[%d].id=%d\n",i,di[i].id);
  54.         }
  55. }
  56. void Fclose(FILE *fp)
  57. {
  58.         printf("%x\n",fp);
  59.         if(fp != NULL)
  60.         {
  61.                 fprintf(stderr,"bug5...\n");
  62.                 fclose(fp);
  63.                 fprintf(stderr,"bug6...\n");
  64.         }
  65. }
复制代码
test_read_record.c
  1. #include "gateway_file.h"

  2. extern DeviceInfo *record;
  3. int main()
  4. {
  5.         int length;

  6.         FILE *fp = Fopen("record.txt");
  7.         fprintf(stderr,"bug1...\n");
  8.         length = ReadFile(fp);
  9.         printf("length in test = %d\n",length);
  10.         fprintf(stderr,"bug2...\n");
  11.         Fprint(record,length);
  12.         fprintf(stderr,"bug3...\n");
  13.         free(record);
  14.         fprintf(stderr,"bug4...\n");
  15.         length = ftell(fp);
  16.         printf("length for ftell = %d\n",length);
  17.         Fclose(fp);

  18.         return 0;
  19. }
复制代码
record.txt

PMAC901 12
PMAC901 12
PMAC901 12
PMAC901 22
PMAC901 32
gateway_file.c封装了读取文件,以及打印数据的一些函数
test_read_record.c为测试文件
gcc test_read_record.c gateway_file.c -o test_read_record
运行./test_read_record,在调用Fclose时,出现段错误


论坛徽章:
324
射手座
日期:2013-08-23 12:04:38射手座
日期:2013-08-23 16:18:12未羊
日期:2013-08-30 14:33:15水瓶座
日期:2013-09-02 16:44:31摩羯座
日期:2013-09-25 09:33:52双子座
日期:2013-09-26 12:21:10金牛座
日期:2013-10-14 09:08:49申猴
日期:2013-10-16 13:09:43子鼠
日期:2013-10-17 23:23:19射手座
日期:2013-10-18 13:00:27金牛座
日期:2013-10-18 15:47:57午马
日期:2013-10-18 21:43:38
2 [报告]
发表于 2015-11-13 11:52 |只看该作者
record = (DeviceInfo*)malloc(len);

这里len应改为记录数*记录大小,跟文件大小不是一回事

论坛徽章:
0
3 [报告]
发表于 2015-11-13 11:59 |只看该作者
你的意思是每一行的大小*行数吗?但是文件内容的大小len不也是这个意思吗?回复 2# hellioncu


   

论坛徽章:
324
射手座
日期:2013-08-23 12:04:38射手座
日期:2013-08-23 16:18:12未羊
日期:2013-08-30 14:33:15水瓶座
日期:2013-09-02 16:44:31摩羯座
日期:2013-09-25 09:33:52双子座
日期:2013-09-26 12:21:10金牛座
日期:2013-10-14 09:08:49申猴
日期:2013-10-16 13:09:43子鼠
日期:2013-10-17 23:23:19射手座
日期:2013-10-18 13:00:27金牛座
日期:2013-10-18 15:47:57午马
日期:2013-10-18 21:43:38
4 [报告]
发表于 2015-11-13 12:01 |只看该作者
回复 3# sagarfan

sizeof(DeviceInfo)*行数

   

论坛徽章:
15
2015七夕节徽章
日期:2015-08-21 11:06:172017金鸡报晓
日期:2017-01-10 15:19:56极客徽章
日期:2016-12-07 14:07:30shanzhi
日期:2016-06-17 17:59:3115-16赛季CBA联赛之四川
日期:2016-04-13 14:36:562016猴年福章徽章
日期:2016-02-18 15:30:34IT运维版块每日发帖之星
日期:2016-01-28 06:20:0015-16赛季CBA联赛之新疆
日期:2016-01-25 14:01:34IT运维版块每周发帖之星
日期:2016-01-07 23:04:26数据库技术版块每日发帖之星
日期:2016-01-03 06:20:00数据库技术版块每日发帖之星
日期:2015-12-01 06:20:00IT运维版块每日发帖之星
日期:2015-11-10 06:20:00
5 [报告]
发表于 2015-11-13 13:39 |只看该作者
gdb用bt打出堆栈信息来看看,感觉不是fclose的错误

论坛徽章:
0
6 [报告]
发表于 2015-11-13 16:37 |只看该作者
这是写越界了,错误原因在于前面朋友所说的申请内存小了。
record = (DeviceInfo*)malloc(len);,

论坛徽章:
0
7 [报告]
发表于 2015-11-13 17:27 |只看该作者
嗯,我是按照char*类型来申请内存的,忘记了结构式是字节对齐的回复 4# hellioncu


   
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP