修改一个类ini文件中某几个变量的值的C代码

同事的一个朋友让她写一个小程序,修改一个文本文件中某几个变量的值(这个文本文件和ini文件比较像,但并不完全符合ini文件的格式,因此不能调用WritePrivateProfileString来修改)。听到这我马上就想说,用Perl或Python来做这事,该是多么简单啊!不过既然现在工作是用VC,就写一段温习温习吧。

  1. #include <stdio.h>  
  2. #include <stdlib.h>  
  3. #include <string.h>  
  4. char *readfile(const char *filename)  
  5. {  
  6.     FILE *file = fopen(filename, "rb");  
  7.     char *buf = NULL;  
  8.     if (file)  
  9.     {  
  10.         fseek(file, 0, SEEK_END);  
  11.         int size = ftell(file);  
  12.         rewind(file);  
  13.         buf = (char*)malloc(size + 1);  
  14.         fread(buf, 1, size, file);  
  15.         fclose(file);  
  16.         buf[size] = 0;  
  17.     }  
  18.     return buf;  
  19. }  
  20. int writefile(const char *filename, const char *buf)  
  21. {  
  22.     FILE *file = fopen(filename, "wb");  
  23.     int done = 0;  
  24.     if (file)  
  25.     {  
  26.         done = fwrite(buf, 1, strlen(buf), file);  
  27.         fclose(file);  
  28.     }  
  29.     return done;  
  30. }  
  31. char *getline(char *buf, char *line)  
  32. {  
  33.     static char *last_buf = NULL;  
  34.     char *p = NULL;  
  35.     if (last_buf != buf)  
  36.     {  
  37.         p = strtok(buf, "/n");  
  38.         last_buf = buf;  
  39.     }  
  40.     else  
  41.     {  
  42.         p = strtok(NULL, "/n");  
  43.     }  
  44.     if (p)  
  45.     {  
  46.         strcpy(line, p);  
  47.     }  
  48.     return p;  
  49. }  
  50. int main(int argc, char* argv[])  
  51. {  
  52.     char *buf = readfile("test.txt");  
  53.     const SECT_COUNT = 2;  
  54.     char *repl_sect[SECT_COUNT*2] = {"server.max-worker""5""server.max-connections""200000"};  
  55.     if (buf)  
  56.     {  
  57.         char *outbuf = (char*)malloc(strlen(buf)*2);  
  58.         *outbuf = 0;  
  59.           
  60.         char *line = (char*)malloc(strlen(buf));  
  61.         while (getline(buf, line))  
  62.         {  
  63.             for (int i=0; i<SECT_COUNT; i++)  
  64.             {  
  65.                 char *p_sect = repl_sect[i*2];  
  66.                 if (strncmp(line, p_sect, strlen(p_sect)) == 0)  
  67.                 {  
  68.                     strcpy(line + strlen(p_sect), " = ");  
  69.                     strcat(line, repl_sect[i*2+1]);  
  70.                     strcat(line, "/r");  
  71.                     break;  
  72.                 }  
  73.             }  
  74.             strcat(line, "/n");  
  75.             strcat(outbuf, line);  
  76.         }  
  77.         writefile("test.txt", outbuf);  
  78.         free(line);  
  79.         free(outbuf);  
  80.         free(buf);  
  81.     }  
  82.     return 0;  

http://blog.csdn.net/ddgg/article/details/3589990

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值