免费注册 查看新帖 |

Chinaunix

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

出现断错误 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2011-03-18 17:11 |只看该作者 |倒序浏览
#include"common.h"
int configuration(int *port ,char *path)
{
        int i;
        char *ch;
        FILE *fp;
        char *p;
        char  buf[1024]="0";
        fp=open("./config.ini",O_RDONLY);
        if(fp==NULL){
                perror("fail to open config.ini");
                return -1;

        }
        ch=fgets(buf,50,fp);//这里出现段错误??????/
        while(ch!=NULL){
                if(buf[strlen(buf)-1]!='\n'){
                        printf("error in config.ini\n");
                        return -1;

                }else
                        buf[strlen(buf)-1]='\0';
                if(strstr(buf,"port")==buf){

                        if((p=strchr(buf,':'))==NULL){
                                printf("config.ini except ':'\n");
                        }
                        *port=atoi(p+2);
                        if(*port<=0){
                                printf("error port\n");
                                return -1;

                        }
                }
                else if (strstr(buf,"root-path")==buf){
                        if((p=strchr(buf,':'))==NULL){
                                printf("config.ini except ':'\n");
                                return -1;
                        }
                        strcpy(path,p);


                }
        }
        return 0;
}

论坛徽章:
0
2 [报告]
发表于 2011-03-18 17:18 |只看该作者
这句,应该用fopen
fp=open("./config.ini",O_RDONLY);

论坛徽章:
0
3 [报告]
发表于 2011-03-18 17:31 |只看该作者
回复 2# marsbible
使用fopen不能打开,返回空指针

论坛徽章:
0
4 [报告]
发表于 2011-03-18 21:56 |只看该作者
  1. fp=fopen("./config.ini","r");
复制代码
??

论坛徽章:
0
5 [报告]
发表于 2011-03-22 17:31 |只看该作者
char  buf[1024]="0";
改成char buf[1024] = {0};

论坛徽章:
0
6 [报告]
发表于 2011-03-22 17:51 |只看该作者
open函数返回的不是FILE* 结构,man一下open 和fopen就明白了

论坛徽章:
0
7 [报告]
发表于 2011-03-28 19:48 |只看该作者
使用fopen()返回空指针说明打开文件失败了,你换成open()返回的是-1,你的判断是成功了,后面fgets()又把-1转为文件指针用,肯定会crash的。

论坛徽章:
0
8 [报告]
发表于 2011-03-29 12:54 |只看该作者
open函数返回的不是FILE* 结构,man一下open 和fopen就明白了
kind_2008 发表于 2011-03-22 17:51



    正确,open的错误时为-1,不是0
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP