ini配置文件读写

 1.定义常量

#define MAX_FILE_SIZE 1024*16

#define LEFT_BRACE  ‘[’;

#define RIGHT_BRACE  ‘]’;

2.读写字符串

 static int load_ini_file(const char *file, char *buf,int *file_size)

{

    FILE *in = NULL;

    int i=0;

    *file_size =0;

    assert(file !=NULL);

    assert(buf !=NULL);

    in = fopen(file,"r");

    if( NULL == in) {return 0;}

    buf[i]=fgetc(in);

    //load initialization file

    while( buf[i]!= (char)EOF) {

          i++;

          assert( i < MAX_FILE_SIZE ); //file too big, you can redefine MAX_FILE_SIZE to fit the big file

          buf[i]=fgetc(in);

     }

    buf[i]=‘/0’;

    *file_size = i;

     fclose(in);

     return 1;

}

    static int newline(char c)

    {

         return ‘/n’ == c ||  ‘/r’; == c )? 1 : 0;

    }

    static int end_of_string(char c)

    {

          return ‘/0’==c? 1 : 0;

     }

     static int left_barce(char c)

     {

          return LEFT_BRACE == c? 1 : 0;

     }

     static int isright_brace(char c )

     {

          return RIGHT_BRACE == c? 1 : 0;

      }

static int parse_file(const char *section, const char *key, const char *buf,int *sec_s,int *sec_e,
  int *key_s,int *key_e, int *value_s, int *value_e)

{

     const char *p = buf;

     int i=0;

     assert(buf!=NULL);

     assert(section != NULL && strlen(section));

     assert(key != NULL && strlen(key));

     *sec_e = *sec_s = *key_e = *key_s = *value_s = *value_e = -1;

     while( !end_of_string(p[i]) ) {

             //find the section

             if( ( 0==i ||  newline(p[i-1]) ) && left_barce(p[i]) )

            {

                     int section_start=i+1;

                    //find the ‘]’;

                   do { i++;} while( !isright_brace(p[i]) && !end_of_string(p[i]));

                   if( 0 == strncmp(p+section_start,section, i-section_start)) {

                   int newline_start=0;

                   i++;

                  //Skip over space char after ‘]’;

                  while(isspace(p[i])) {i++;}

                  //find the section

                 *sec_s = section_start;

                  *sec_e = i;

                  while( ! (newline(p[i-1]) && left_barce(p[i])) && !end_of_string(p[i]) ) {

                                int j=0;

                               //get a new line

                               newline_start = i;

                               while( !newline(p[i]) &&  !end_of_string(p[i]) ) {i++;}

                               //now i  is equal to end of the line

                                j = newline_start;

                                if(‘;’ != p[j]) //skip over comment

                                {

                                          while(j < i && p[j]!=&apos;=&apos;) {

                                                       j++;

                                                       if(&apos;=&apos; == p[j]) {

                                                              if(strncmp(key,p+newline_start,j-newline_start)==0){

                                                                       //find the key ok

                                                                       *key_s = newline_start;

                                                                        *key_e = j-1;

                                                                        *value_s = j+1;

                                                                        *value_e = i;

                                                                         return 1;

                                                               }

                                      }

                              }

                     }

                  i++;

             }

       }

  }

else{i++;}

}

     return 0;

int read_profile_string(const char *file,const char *section, const char *key,
  char *value, int size, const char *default_value)
 {
  char buf[MAX_FILE_SIZE]={0};
  int file_size;
  int sec_s,sec_e,key_s,key_e, value_s, value_e;

  //check parameters
  assert(section != NULL && strlen(section));
  assert(key != NULL && strlen(key));
  assert(value != NULL);
  assert(size > 0);
  assert(file !=NULL &&strlen(key));

  if(!load_ini_file(file,buf,&file_size))
  {
   if(default_value!=NULL)
   {
    strncpy(value,default_value, size);
   }
   return 0;
  }

  if(!parse_file(section,key,buf,&sec_s,&sec_e,&key_s,&key_e,&value_s,&value_e))
  {
   if(default_value!=NULL)
   {
    strncpy(value,default_value, size);
   }
   return 0; //not find the key
  }
  else
  {
   int cpcount = value_e -value_s;

   if( size-1 < cpcount)
   {
    cpcount =  size-1;
   }

   memset(value, 0, size);
   memcpy(value,buf+value_s, cpcount );
   value[cpcount] = &apos;/0&apos;;

   return 1;
  }
 }

 int write_profile_string(const char *file,const char *section,
  const char *key,const char *value)
 {
  char buf[MAX_FILE_SIZE]={0};
  char w_buf[MAX_FILE_SIZE]={0};
  int sec_s,sec_e,key_s,key_e, value_s, value_e;
  int value_len = (int)strlen(value);
  int file_size;
  FILE *out;

  //check parameters
  assert(section != NULL && strlen(section));
  assert(key != NULL && strlen(key));
  assert(value != NULL);
  assert(file !=NULL &&strlen(key));

  if(!load_ini_file(file,buf,&file_size))
  {
   sec_s = -1;
  }
  else
  {
   parse_file(section,key,buf,&sec_s,&sec_e,&key_s,&key_e,&value_s,&value_e);
  }

  if( -1 == sec_s)
  {
   if(0==file_size)
   {
    sprintf(w_buf+file_size,"[%s]/n%s=%s/n",section,key,value);
   }
   else
   {
    //not find the section, then add the new section at end of the file
    memcpy(w_buf,buf,file_size);
    sprintf(w_buf+file_size,"/n[%s]/n%s=%s/n",section,key,value);
   }
  }
  else if(-1 == key_s)
  {
   //not find the key, then add the new key=value at end of the section
   memcpy(w_buf,buf,sec_e);
   sprintf(w_buf+sec_e,"%s=%s/n",key,value);
   sprintf(w_buf+sec_e+strlen(key)+strlen(value)+2,buf+sec_e, file_size - sec_e);
  }
  else
  {
   //update value with new value
   memcpy(w_buf,buf,value_s);
   memcpy(w_buf+value_s,value, value_len);
   memcpy(w_buf+value_s+value_len, buf+value_e, file_size - value_e);
  }

  out = fopen(file,"w");
  if(NULL == out)
  {
   return 0;
  }

  if(-1 == fputs(w_buf,out) )
  {
   fclose(out);
   return 0;
  }

  fclose(out);
  return 1;
 }

3.读写整数

 int read_profile_int(const char *file, const char *section,
  const char *key,int default_value)
 {
  char value[32] = {0};
  if(!read_profile_string(file,section,key,value, sizeof(value),NULL))
  {
   return default_value;
  }
  else
  {
   return atoi(value);
  }
 }

 int write_profile_int(const char *file,const char *section,
  const char *key,int *value)
 {
  char buf[MAX_FILE_SIZE]={0};
  _itoa(*value,buf,10);
  return write_profile_string(file,section,key,buf);
 }

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
c ini配置文件读写是一种常见的配置文件读写方式。在C语言中,通常使用文件操作函数来读写ini配置文件。 读取ini文件首先需要打开文件,可以使用fopen函数打开文件,并指定打开方式为"r"(只读方式)。然后逐行读取文件内容,可以使用fgets函数逐行读取。读取到的每一行字符串都可以通过字符串处理函数进行进一步操作,例如使用strtok函数将行字符串分割成键值对。 对于每一行的键值对,可以进一步使用字符串处理函数进行解析。可以使用strchr函数找到等号(=)的位置,将键和值分隔开。然后可以使用strcpy或strncpy函数将键和值分别复制到变量中,并进行相应的后续处理。 写入ini文件也需要打开文件,可以使用fopen函数打开文件,并指定打开方式为"w"(写入方式)。然后可以使用fprintf函数将配置项写入文件。具体的操作是先写入键的字符串,然后写入等号(=),最后写入值的字符串。写入完毕后,可以使用fclose函数关闭文件。 需要注意的是,在读取和写入ini文件时,需要进行错误处理,例如检查文件是否打开成功、是否成功读写、文件关闭时是否出错等。这样可以保证程序的健壮性。 总之,对于C语言来说,ini配置文件读写是一种比较简单和常见的操作,通过使用文件操作函数和字符串处理函数,可以方便地读取和写入ini文件中的配置项。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值