新书推介:《语义网技术体系》
作者:瞿裕忠,胡伟,程龚
   XML论坛     W3CHINA.ORG讨论区     计算机科学论坛     SOAChina论坛     Blog     开放翻译计划     新浪微博  
 
  • 首页
  • 登录
  • 注册
  • 软件下载
  • 资料下载
  • 核心成员
  • 帮助
  •   Add to Google

    >> 本版讨论高级C/C++编程、代码重构(Refactoring)、极限编程(XP)、泛型编程等话题
    [返回] 中文XML论坛 - 专业的XML技术讨论区计算机技术与应用『 C/C++编程思想 』 → 文件操作,將一個文件中的特定值插入到另一文件的指定處 查看新帖用户列表

      发表一个新主题  发表一个新投票  回复主题  (订阅本版) 您是本帖的第 13239 个阅读者浏览上一篇主题  刷新本主题   树形显示贴子 浏览下一篇主题
     * 贴子主题: 文件操作,將一個文件中的特定值插入到另一文件的指定處 举报  打印  推荐  IE收藏夹 
       本主题类别:     
     卷积内核 帅哥哟,离线,有人找我吗?
      
      
      威望:8
      头衔:总统
      等级:博士二年级(版主)
      文章:3942
      积分:27590
      门派:XML.ORG.CN
      注册:2004/7/21

    姓名:(无权查看)
    城市:(无权查看)
    院校:(无权查看)
    给卷积内核发送一个短消息 把卷积内核加入好友 查看卷积内核的个人资料 搜索卷积内核在『 C/C++编程思想 』的所有贴子 访问卷积内核的主页 引用回复这个贴子 回复这个贴子 查看卷积内核的博客楼主
    发贴心情 文件操作,將一個文件中的特定值插入到另一文件的指定處


    int GetValueInFile(char * fileName,char *bufferGet,char *locationStr,int xIndex)
    {
     FILE * fpSource;
     char msgShow[256] = ""; 

     int length = strlen(CurrentPath)+strlen(fileName);
     memset(bufferGet,0,sizeof(bufferGet));

     char * soureFileName = new char [length];
     memset(soureFileName,0,length);
     strcat(soureFileName,CurrentPath);
     strcat(soureFileName,fileName);

     if ( !(fpSource=fopen(soureFileName,"r") ) )
     {
      sprintf(msgShow,"\nFile Define In Config can't be read :\n%s\n",soureFileName);
      help(msgShow);
      return 1;
     }

     int xCount = 0 ;
     bool GotThat = false;
     while ( !feof(fpSource) )
     {
      char buffer[256] = "";
      fgets(buffer,256,fpSource);   
      if ( strstr(buffer,locationStr)>0 )
      {  
       xCount+=1;
       int xlocation = strstr(buffer,locationStr)-buffer;
       if ( xIndex==xCount )
       {         
        int xBegin=xlocation+strlen(locationStr);
        int xEnd= xBegin;
        int length = strlen(buffer);

        for ( ;  buffer[xBegin]==' '; xBegin++ )
         ;
        for ( xEnd= xBegin; (buffer[xEnd]!=' ')&&(buffer[xEnd]!='\n');xEnd++ )
         ;
        if ( xEnd>xBegin )
        {
         GotThat = true; 
         strncpy(bufferGet,buffer+xBegin,(xEnd-xBegin));
         printf("\nGet Value OK:\n%s\n",bufferGet);     
        } 
        else
        {
         memset(msgShow,0,256);
         sprintf(msgShow,"\nThe %dst string By keyword: \n%s\nFound but is Null in File: \n%s\n",
           xIndex,locationStr,fileName);  
         printf(msgShow);
        }
        break;
       }    
      } 
     }
     if ( !GotThat )
     {
      memset(msgShow,0,256);
      sprintf(msgShow,"\nThe %dst string By keyword: \n%s\nNot found in File: \n%s",
        xIndex,locationStr,fileName);
      help(msgShow);  
     }
       
     fclose(fpSource);

     return GotThat?0:1;
    }

    int AddValueToFile(char * fileName,char *valueWrite,char *locationStr,int xIndex)
    {
     FILE * fpSource;
     FILE * fpDest;
     char msgShow[256] = "";
     
     int length = strlen(CurrentPath)+strlen(fileName)+5;
     char * tmpFileName = new char [length];
     char * soureFileName = new char [length];
     memset(tmpFileName,0,length);
     memset(soureFileName,0,length);
     sprintf(tmpFileName,"%s%s_tmp",CurrentPath,fileName);
     strcat(soureFileName,CurrentPath);
     strcat(soureFileName,fileName);

     remove(tmpFileName); 

     if ( !(fpSource=fopen(soureFileName,"r") ) )
     {
      sprintf(msgShow,"\nFile Define In Config can't be read :\n%s\n",fileName);
      help(msgShow);
      return 1;
     }

     if (!( fpDest=fopen(tmpFileName,"w")))
     {  
      help("\nCan't Create New File in current locatioin!");
      return 1;
     } 

     int xCount = 0 ;
     bool GotThat = false;
     while ( !feof(fpSource) )
     {
      char buffer[256] = "";
      fgets(buffer,256,fpSource);
      if ( !GotThat )
      {
       if ( strstr(buffer,locationStr)>0 )
       {    
        xCount+=1;
        if ( xIndex==xCount )
        {
         GotThat = true;
         
         int xlocation = strstr(buffer,locationStr)-buffer;   
         char writeBuffer[256] = "";
         strncpy(writeBuffer,buffer,xlocation+strlen(locationStr));
         fputs(writeBuffer,fpDest);
         fputs(valueWrite,fpDest);

         memset(writeBuffer,0,256);
         int xBegin = 0;
         for ( xBegin= xlocation+strlen(locationStr);
           (buffer[xBegin]!=' ')&&(buffer[xBegin]!='\n');
           xBegin++ )
          ;

         strcpy(writeBuffer,buffer+xBegin);
         fputs(writeBuffer,fpDest);     
        }
        else
         fputs(buffer,fpDest);
       }
       else
        fputs(buffer,fpDest);
      }
      else
       fputs(buffer,fpDest);
     }

     fclose(fpSource);
     fclose(fpDest);

     if ( GotThat )
     {    
      if ( 0!=remove(soureFileName) )
      {
       memset(msgShow,0,256);
       sprintf(msgShow,"\nCan't remove file:\n%s\n",soureFileName);  
       help(msgShow);  
       printf("ERROR Code:%d\n",errno);
       return 1;
      }
     
      if (0!=rename(tmpFileName,soureFileName))
      {
       
       memset(msgShow,0,256);
       sprintf(msgShow,"\nCan't Rename in current locatioin!\n%s",soureFileName);  
       printf("ERROR Code:%d",errno);
       help(msgShow);
       return 1;
      }   
      printf("\nSet Value OK\n"); 
     }
     else
     {
      remove(tmpFileName); 
      memset(msgShow,0,256);
      sprintf(msgShow,"\nThe %dst string By keyword: \n%s\nNot found in File: \n%s",
         xIndex,locationStr,soureFileName);
      help(msgShow);  
     }

     return GotThat?0:1;
    }


       收藏   分享  
    顶(0)
      




    ----------------------------------------------
    事业是国家的,荣誉是单位的,成绩是领导的,工资是老婆的,财产是孩子的,错误是自己的。

    点击查看用户来源及管理<br>发贴IP:*.*.*.* 2011/9/30 14:20:00
     
     GoogleAdSense
      
      
      等级:大一新生
      文章:1
      积分:50
      门派:无门无派
      院校:未填写
      注册:2007-01-01
    给Google AdSense发送一个短消息 把Google AdSense加入好友 查看Google AdSense的个人资料 搜索Google AdSense在『 C/C++编程思想 』的所有贴子 访问Google AdSense的主页 引用回复这个贴子 回复这个贴子 查看Google AdSense的博客广告
    2024/11/11 0:16:10

    本主题贴数1,分页: [1]

    管理选项修改tag | 锁定 | 解锁 | 提升 | 删除 | 移动 | 固顶 | 总固顶 | 奖励 | 惩罚 | 发布公告
    W3C Contributing Supporter! W 3 C h i n a ( since 2003 ) 旗 下 站 点
    苏ICP备05006046号《全国人大常委会关于维护互联网安全的决定》《计算机信息网络国际联网安全保护管理办法》
    61.951ms