免费注册 查看新帖 |

Chinaunix

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

从文件读取行,存储于struct中,返回指针变量出错,请大家帮看看 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2004-03-05 16:21 |只看该作者 |倒序浏览
<code>;
#define _GNU_SOURCE
#include <stdio.h>;
#include <unistd.h>;
#include <string.h>;
#include <stdlib.h>;
#include <time.h>;
#include <signal.h>;



struct vgetconf {
  char *conf_card;                /* card  */
  char *conf_server_domain;                /* server domain name  */
  char *conf_clint_domain;                /* client domain name  */
char *conf_local_host;                /* localhost */
  char *conf_remote_host;                /* remote host */
   char *conf_local_area;                /*  localarea  */
   char *conf_remote_area;                /* remote area  */
};

struct vgetconf   *getline_b()

       {
struct vgetconf vgetconfs;
          FILE * fp;
            char * line = NULL;
            size_t len = 0;
            ssize_t read;
                        int i;
char *vget[6];

            fp = fopen("b.conf", "r";
            if (fp == NULL)
                 exit(EXIT_FAILURE);
            for(i=0;i<7;i++){
                read = getline(&line, &len, fp);      
                           vget=(char *)malloc(strlen(line)+1);     
                          strcpy(vget,line);   
                           }
            if (line)
                 free(line);
        vgetconfs.conf_card=vget[0];               
   vgetconfs.conf_server_domain=vget[1];               
   vgetconfs.conf_clint_domain=vget[2];               
   vgetconfs.conf_local_host=vget[3];               
   vgetconfs.conf_remote_host=vget[4];               
   vgetconfs.conf_local_area=vget[5];               
   vgetconfs.conf_remote_area=vget[6];
  return &amp;
       }


main()
{
struct vgetconf *vgetconfm;
vgetconfm=getline_b();


printf("%s \n", vgetconfm->;conf_card);
  printf("%s \n", vgetconfm->;conf_server_domain);
  printf("%s \n", vgetconfm->;conf_clint_domain);
  printf("%s \n", vgetconfm->;conf_local_host);
  printf("%s \n", vgetconfm->;conf_remote_host);
  printf("%s \n", vgetconfm->;conf_local_area);
  printf("%s \n", vgetconfm->;conf_remote_area);
  return EXIT_SUCCESS;

}
</code>;

b.conf内容如下:
eth0
main.a.com
client.a.com
10.20.40.243
10.20.6.35
10.20.40.0
10.20.6.0


编译信息:
gcc -o testfile testprintfi.c
testprintfi.c: In function `getline_b':
testprintfi.c:50: warning: function returns address of local variable
testprintfi.c:69:2: warning: no newline at end of file

执行后,显示到终端是三行乱码
è™`üÿÃ 4
à'Bp]@°Â
xéÿ¿WB

论坛徽章:
1
荣誉版主
日期:2011-11-23 16:44:17
2 [报告]
发表于 2004-03-05 16:25 |只看该作者

从文件读取行,存储于struct中,返回指针变量出错,请大家帮看看

testprintfi.c:50: warning: function returns address of local variable
看这一句警告吧。不要说你看不懂。
去你的程序那一行看看怎么回事。

论坛徽章:
0
3 [报告]
发表于 2004-03-05 16:42 |只看该作者

从文件读取行,存储于struct中,返回指针变量出错,请大家帮看看

static struct vgetconf vgetconfs;
大家以后别犯我同样错误咯~要注意变量生存期问题和有效范围!

希望此帖可对初学者读取文件有参考作用!

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

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP