Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1177370
  • 博文数量: 573
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 66
  • 用 户 组: 普通用户
  • 注册时间: 2016-06-28 16:21
文章分类

全部博文(573)

文章存档

2018年(3)

2016年(48)

2015年(522)

分类: C/C++

2015-12-02 16:30:26


点击(此处)折叠或打开

  1. #include <sys/stat.h>
  2. #include <sys/types.h>
  3. #include <time.h>
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6. #include <string.h>
  7. #include <unistd.h>

  8. int GetConfigValue(char *sFile,char *sField,char *sVal);
  9. int modifycfg(char *sFile,char *sField,char *sVal);
  10. char * StrTrim(char * pstr);

  11. int main(int argc, char ** argv)
  12. {
  13.     char    num1value[128];
  14.     char    num2value[128];
  15.     char    addvalue[128];
  16.     char    filename[128];
  17.     
  18.     memset(addvalue, 0, sizeof(addvalue));
  19.     memset(filename, 0 ,sizeof(filename));
  20.     
  21.     if(argc < 4)
  22.     {
  23.         printf("参数个数不对!\n");
  24.         return -1;
  25.     }
  26.     strcpy(filename, argv[1]);
  27.     memset(num1value, 0, sizeof(num1value));
  28.     strcpy(num1value, argv[2]);
  29.     if(modifycfg(filename, "num1", num1value) < 0)
  30.         return -1;
  31.     memset(num2value, 0, sizeof(num2value));
  32.     strcpy(num2value, argv[3]);
  33.     if(modifycfg(filename, "num2", num2value) < 0)
  34.         return -1;
  35.     
  36.     memset(num1value, 0, sizeof(num1value));
  37.     if(GetConfigValue(filename, "num1", num1value) < 0)
  38.         return -1;
  39.     StrTrim(num1value);
  40.     memset(num2value, 0, sizeof(num2value));
  41.     if(GetConfigValue(filename, "num2", num2value) < 0)
  42.         return -1;
  43.     StrTrim(num2value);
  44.     
  45.     int add = atoi(num1value) + atoi(num2value);
  46.     sprintf(addvalue, "%d", add);
  47.     if(modifycfg(filename, "add", addvalue) < 0)
  48.         return -1;
  49.     
  50.     printf("num1 + num2 = add\n");
  51.     printf("[%s] + [%s] = [%s]\n", num1value, num2value, addvalue);
  52.     
  53.     FILE * fp = NULL;
  54.     char    sTotalBuf[1024*1000*8];        /*存放全部文件内容*/
  55.     char    sChildBuf[128];                /*存放一次读取的文件内容*/
  56.     if(JugeFileExist(filename) != 0)
  57.     {
  58.         printf("文件[%s]不存在!\n", filename);
  59.         return -1;
  60.     }
  61.     if((fp = fopen(filename, "r")) == NULL)
  62.     {
  63.         printf("打开文件[%s]失败\n", filename);
  64.         return -1;
  65.     }
  66.     memset(sTotalBuf, 0, sizeof(sTotalBuf));
  67.     memset(sChildBuf, 0, sizeof(sChildBuf));
  68.     while(NULL != fgets(sChildBuf, sizeof(sChildBuf), fp))
  69.     {
  70.         strcat(sTotalBuf, sChildBuf);
  71.         memset(sChildBuf, 0, sizeof(sChildBuf));
  72.     }
  73.     StrTrim(sTotalBuf);
  74.     printf("文件[%s],内容如下:\n%s\n", filename, sTotalBuf);
  75.     
  76.     return 0;
  77. }

  78. /*修改文件内容*/
  79. int modifycfg(char *sFile,char *sField,char *sVal)
  80. {
  81.     char sName[1024];
  82.     char sBuf[1024];
  83.     char sValue[1024];
  84.     char *p = NULL;
  85.     FILE *fp = NULL;
  86.     
  87.     int len = -1;
  88.     int len_value = -1;
  89.     char space[1024];
  90.     char tempName[128];
  91.     char tempSbuf[1024];

  92.     if(JugeFileExist(sFile) != 0)
  93.     {
  94.         printf("文件[%s]不存在!\n", sFile);
  95.         return -1;
  96.     }
  97.     
  98.     fp = fopen(sFile,"r+");
  99.     if(NULL == fp)
  100.     {
  101.         printf("fopen file[%s] failed!\n",sFile);
  102.         return -1;
  103.     }

  104.     while(!feof(fp))
  105.     {
  106.         memset(sBuf,0x00,sizeof(sBuf));
  107.         memset(sName,0x00,sizeof(sName));
  108.         memset(sValue,0x00,sizeof(sValue));
  109.         fgets(sBuf,sizeof(sBuf),fp);
  110.         memset(tempSbuf, 0, sizeof(tempSbuf));
  111.         strcpy(tempSbuf, sBuf);
  112.         StrTrim(sBuf);
  113.         p = strchr(sBuf,'#');
  114.         if(p != NULL)/*#后面是注释,过滤*/
  115.         {
  116.             *p = '\0';
  117.         }
  118.         if(strlen(sBuf) == 0)
  119.             continue;
  120.         p = strchr(sBuf,'=');
  121.         if(p != NULL)
  122.         {
  123.             memcpy(sName,sBuf,p-sBuf);
  124.         }
  125.         
  126.         memset(tempName, 0, sizeof(tempName));
  127.         strcpy(tempName, sName);
  128.         StrTrim(sName);

  129.         strcpy(sValue,p+1);
  130.         if(strcmp(sName,sField) == 0)
  131.         {
  132.             len = 0 - strlen(tempSbuf) + strlen(tempName) + 1;    
  133.             memset(space, 0, sizeof(space));
  134.             len_value = strlen(sValue);
  135.             memset(space, ' ', len_value);
  136.             fseek(fp, len, SEEK_CUR);
  137.             fputs(space, fp);
  138.             len_value = 0 - len_value;
  139.             fseek(fp, len_value, SEEK_CUR);
  140.             fputs(sVal, fp);
  141.             break;
  142.         }
  143.     }
  144.     fclose(fp);
  145.     return 0;
  146. }

  147. /*获取配置信息*/
  148. int GetConfigValue(char *sFile,char *sField,char *sVal)
  149. {
  150.     char sName[1024];
  151.     char sBuf[1024];
  152.     char sValue[1024];
  153.     char *p = NULL;
  154.     FILE *fp = NULL;

  155.     if(JugeFileExist(sFile) != 0)
  156.     {
  157.         printf("文件[%s]不存在!\n", sFile);
  158.         return -1;
  159.     }
  160.     fp = fopen(sFile,"r");
  161.     if(NULL == fp)
  162.     {
  163.         printf("fopen file[%s] failed!\n",sFile);
  164.         return -1;
  165.     }

  166.     while(!feof(fp))
  167.     {
  168.         memset(sBuf,0x00,sizeof(sBuf));
  169.         memset(sName,0x00,sizeof(sName));
  170.         memset(sValue,0x00,sizeof(sValue));
  171.         fgets(sBuf,sizeof(sBuf),fp);
  172.         StrTrim(sBuf);
  173.         p = strchr(sBuf,'#');
  174.         if(p != NULL)/*#后面是注释,过滤*/
  175.         {
  176.             *p = '\0';
  177.         }
  178.         if(strlen(sBuf) == 0)
  179.             continue;
  180.         p = strchr(sBuf,'=');
  181.         if(p != NULL)
  182.         {
  183.             memcpy(sName,sBuf,p-sBuf);
  184.         }
  185.         StrTrim(sName);

  186.         strcpy(sValue,p+1);
  187.         StrTrim(sValue);
  188.         if(strcmp(sName,sField) == 0)
  189.         {
  190.             strcpy(sVal,sValue);
  191.             break;
  192.         }
  193.     }
  194.     fclose(fp);
  195.     return 0;
  196. }


  197. char * StrTrim(char * pstr)
  198. {
  199.     if(NULL == pstr)
  200.     {
  201.         return NULL;
  202.     }
  203.     char * phead = pstr;
  204.     char * ptail = pstr + strlen(pstr) - 1;
  205.     while((*phead == ' ' || *phead == '\t' || *phead == '\n' || *phead == '\b' || *phead == '\r')&&(phead < ptail)) phead++;
  206.     while((*ptail == ' ' || *ptail == '\t' || *ptail == '\n' || *ptail == '\b' || *ptail == '\r')&&(ptail > phead)) ptail--;
  207.     *(ptail + 1) = '\0';
  208.     memcpy(pstr, phead, sizeof(char)*(ptail - phead + 2));
  209.     return pstr;
  210. }

  211. /***********************************************************
  212.  * 函 数: JugeFileExist()
  213.  * 功能描述: 判断文件是否存在
  214.  * 输入参数:lFilename    -    长文件名
  215.  * 返 回:0-文件存在;1-文件不存在
  216.  * 流程描述:
  217.  *    说 明:
  218.  * 修改记录:
  219.  * [修改人] [日期][描述]
  220. ***********************************************************/
  221. int JugeFileExist(char *lFileName)
  222. {
  223.     struct stat statbuf;
  224.     memset(&statbuf,0,sizeof(statbuf));
  225.     stat(lFileName,&statbuf);
  226.     if(statbuf.st_mode & S_IFREG)
  227.         return 0;
  228.     else
  229.         return 1;
  230. }

阅读(415) | 评论(0) | 转发(0) |
0

上一篇:printf格式化打印

下一篇:getcwd函数

给主人留下些什么吧!~~