免费注册 查看新帖 |

Chinaunix

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

【求助】把一个配置文件放到结构体中,然后打印出来 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2010-04-28 13:15 |只看该作者 |倒序浏览
本帖最后由 凡夫子 于 2010-04-28 13:16 编辑

配置文件/etc/1.conf为
filename=/a/log
id=123
acess=no

论坛徽章:
0
2 [报告]
发表于 2010-04-28 13:37 |只看该作者
希望大家帮帮忙咯

论坛徽章:
0
3 [报告]
发表于 2010-04-28 15:15 |只看该作者
#include <stdio.h>
#include <string.h>

struct cfg
{
    char filename[100];
    short access;
    unsigned long id;
};

short read_cfg( FILE *fd, struct cfg *config )
{
      if( fread( config, sizeof(struct cfg), 1, fd )  <= 0 )
              return 0;
      return 1;
}

short write_cfg( FILE *fd, struct cfg *config )
{
      if( fwrite( config, sizeof(struct cfg), 1, fd )  <= 0 )
              return 0;
      return 1;
}

int main( int argc, char **argv )
{
       FILE *read_fd, *write_fd;
       struct cfg r_cfg, w_cfg;
       read_fd = fopen( "/ect/1.conf", "r" );
       write_fd = fopen( "/ect/2.conf", "w+" );
       if( !read_fd  || !write_fd  )
             return -1;
       memset( &r_cfg, 0, sizeof(struct cfg) );
       memset( &w_cfg, 0, sizeof(struct cfg) );
       if( !read_cfg( read_fd, &r_cfg ) )
       {
             printf( "Can't read config!\n" );
             return -1;
       }
       printf( "read config, filename: %s, id: %lu, access: %d\n", r_cfg.filename, r_cfg.id, r_cfg.access );
       strcpy( w_cfg.filename, "any" );
       w_cfg.id = 456;
       w_cfg.access = 1;
       if( !write_cfg(  write_fd,  &w_cfg ) )
       {
            printf( "Can't write config!\n" );
             return -1;
       }
       printf( "Write config done!\n" );
       fclose(read_fd);
       fclose(write_fd);
}

论坛徽章:
0
4 [报告]
发表于 2010-04-30 10:46 |只看该作者
本帖最后由 niko_zju 于 2010-04-30 10:52 编辑

给个实用点的例子,摘自 iscsi-scst
配置文件格式如下:
Target iqn.2005-01.com.test:32b7d2b2acda



函数如下:
  1. static int config_main_init(char *filename)
  2. {
  3.         FILE *config;
  4.         char buf[BUFSIZE];
  5.         char *p, *q;
  6.         int idx;
  7.         u32 tid, val;
  8.         int res = 0;

  9.         if (!(config = fopen(filename, "r"))) {
  10.                 return errno == ENOENT ? 0 : -errno;
  11.         }

  12.         tid = 0;
  13.         while (fgets(buf, BUFSIZE, config)) {
  14.                 q = buf;
  15.                 p = target_sep_string(&q);
  16.                 if (!p || *p == '#')
  17.                         continue;
  18.                 if (!strcasecmp(p, "Target")) {
  19.                         tid = 0;
  20.                         if (!(p = target_sep_string(&q)))
  21.                                 continue;
  22.                         if (__config_target_create(&tid, p, 0))
  23.                                 log_debug(1, "creating target %s", p);
  24.                 } else if (!strcasecmp(p, "Alias") && tid) {
  25.                         ;
  26.                 } else if (!((idx = param_index_by_name(p, target_keys)) < 0) && tid) {
  27.                         val = strtol(q, &q, 0);
  28.                         if (param_check_val(target_keys, idx, &val) < 0) {
  29.                                 log_error("Wrong value %u for parameter %s\n",
  30.                                         val, target_keys[idx].name);
  31.                                 res = -1;
  32.                                 break;
  33.                         }
  34.                         iscsi_param_partial_set(tid, 0, key_target, idx, val);
  35.                 } else if (!((idx = param_index_by_name(p, session_keys)) < 0) && tid) {
  36.                         char *str = target_sep_string(&q);
  37.                         if (param_str_to_val(session_keys, idx, str, &val) < 0) {
  38.                                 log_error("Wrong value %s for parameter %s\n",
  39.                                         str, session_keys[idx].name);
  40.                                 res = -1;
  41.                                 break;
  42.                         }
  43.                         if (param_check_val(session_keys, idx, &val) < 0) {
  44.                                 log_error("Wrong value %u for parameter %s\n",
  45.                                         val, session_keys[idx].name);
  46.                                 res = -1;
  47.                                 break;
  48.                         }
  49.                         iscsi_param_partial_set(tid, 0, key_session, idx, val);
  50.                 } else if (param_index_by_name(p, user_keys) < 0) {
  51.                         log_warning("Unknown osniscst.conf param: %s\n", p);
  52.                         res = -1;
  53.                         break;
  54.                 }
  55.         }

  56.         fclose(config);
  57.         return res;
  58. }
复制代码

  1. static char *target_sep_string(char **pp)
  2. {
  3.         char *p = *pp;
  4.         char *q;

  5.         for (p = *pp; isspace(*p); p++)
  6.                 ;
  7.         for (q = p; *q && !isspace(*q); q++)
  8.                 ;
  9.         if (*q)
  10.                 *q++ = 0;
  11.         else
  12.                 p = NULL;
  13.         *pp = q;
  14.         return p;
  15. }
复制代码

论坛徽章:
0
5 [报告]
发表于 2010-04-30 14:46 |只看该作者
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define M 30
int main()
{
  struct struct_a
  {
    char filename[M];
    int id;
    char acess[M];
  }s_var;

  FILE *fp;
  fp = fopen("/etc/1.conf","r");
  if(fp == NULL)
    {
       puts("The file you opened is not found.\n");
       exit(1);
    }

  char buf1[M];
  char a[M];
  int i;

  while(1){
  if( fgets(buf1, M, fp) == NULL )
        break;
  if( strstr(buf1, "filename=") ){
        strncpy(s_var.filename, buf1+strlen("filename="), M-1);
        printf("s_var.filename=%s", s_var.filename);
    }
   memset(a,0x00,sizeof(a));
  if( strstr(buf1, "id=") ){
        strncpy(a, buf1+strlen("id="), M-1);
        i = atoi(a);
        s_var.id = i;
        printf("s_var.id=%d\n", s_var.id);
   }
if( strstr(buf1, "acess=" )){
        strncpy(s_var.acess, buf1+strlen("acess="), M-1);
        printf("s_var.acess=%s", s_var.acess);
  }
}
  fclose(fp);
  exit(0);
}
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP