I have written this simple C program that changes the rotation file name. Eg: For A_B_C_1 Need to rotate it to A_B_C_2 and if A_B_C need to rotate it to A_B_C_1 Problem is the strncat is not working as expectedly. It is giving me output like: A_B_C_1 (Works fine for single instance) A_B_C_2 A_B_C_23 A_B_C_2324 Logic used is simple:get the last digit in the string if it exists. If it does not simply append _1 (This one is working fine) If it does - extract the number, increment it and append to the new string. Is there some std library that does this? Gives error on calling this method in a loop/multiple times....
원인을 디버그 할 수 없습니다. 도움이 필요하면 안내하십시오. 로그 파일 순환 이름
int getRotationFileName(char *sFileName,char *sNewFileName) { char sTmpFile[256]; int newRotation; memset(sTmpFile,NULL,sizeof(sTmpFile)); strncpy(sTmpFile, sFileName, strlen(sFileName)); char *tokenPtr; strtok(sFileName,"_"); //a strtok(NULL, "_"); //b strtok(NULL, "_"); //c tokenPtr = strtok(NULL, "_"); //1 printf("sTempFile [%s], sFileName [%s], token [%s]", sTmpFile,sFileName,tokenPtr); if(tokenPtr!= NULL)//Last - exists { newRotation = atoi(tokenPtr); int newLen = strlen(sTmpFile); int oneLen = strlen(tokenPtr); memset(sNewFileName, NULL, sizeof(sNewFileName)); printf("sNewFileName is prior: %s and len is %d \n", sNewFileName, (newLen-oneLen)); printf("sTempName is prior: %s", sTmpFile); strncpy(sNewFileName,sTmpFile, (newLen-oneLen)); printf("diff is %d\n", (newLen-oneLen)); printf("sNewFileName before concat %s \n", sNewFileName); newRotation++; sprintf(sNewFileName,"%s%d",sNewFileName, newRotation); sNewFileName[strlen(sNewFileName)]='\0'; printf("sNewFileName after concat %s \n", sNewFileName); } else { printf("in else TmpFile [%s] , New File [%s], len %d",sTmpFile, sNewFileName,strlen(sTmpFile)); strcat(sTmpFile,"_1"); strncpy(sNewFileName,sTmpFile, strlen(sTmpFile)); } strcpy(sFileName, sNewFileName); printf("\nNew file created is %s\n",sNewFileName); return 1; }는 라인에 문제가있는 것 같다 :를 strncpy (sNewFileName, sTmpFile (newLen-oneLen));피드백 : 코드 포매터는 크롬 브라우저
소스 코드의 형식이 꽤 엉망입니다 ... 코드를 게시하려면 4 개의 공백을 들여 쓰기 만하면됩니다. –
나는 그것을 고쳤지만 수정 사항이 되돌려졌다. Chrome의 "feedback"라인이 의미하는 바라고 생각한다. 기묘한. 수정 된 버전이 수정 내역에 있으므로 다시 적용하십시오. :) – unwind
작은 업데이트 : 초기 이름에 숫자가있을 수 있으며 필요한 경우 증분 된 숫자로 마지막 밑줄을 추가하기 전에 두 개의 밑줄을 확인해야합니다. – techzen