-- 作者:卷积内核
-- 发布时间:9/30/2011 2:20:00 PM
-- 文件操作,將一個文件中的特定值插入到另一文件的指定處
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; }
|