代码之家  ›  专栏  ›  技术社区  ›  techzen

日志文件轮换名称

  •  0
  • techzen  · 技术社区  · 17 年前

    A_B_C_1 A_B_C_2 A_B_C A_B_C_1

    strncat

    A_B_C_1 (Works fine for single instance)
    A_B_C_2
    A_B_C_23
    A_B_C_2324
    

    -获取字符串中的最后一位数字(如果存在)。

    有什么std库可以做到这一点吗?

       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;
    

    }

    6 回复  |  直到 7 年前
        1
  •  1
  •   unwind    17 年前

    • 递增数字。
    • 通过连接前缀和数字(作为字符串)来创建新字符串。

    请注意,这些算法很生动,通常最好让标准库为您生成一个唯一的名称。

    这里有一个解决方案:

    void rotateName(const char *oname, char *nname)
    {
        const char  *ptr;
        int         number, plen;
    
        for(ptr = oname; *ptr && !isdigit(*ptr); ptr++)
                ;
        if(*ptr == '\0')
                number = 0;
        else
                number = atoi(ptr);
        plen = ptr - oname;
        if(plen <= 0)
                return;
        memcpy(nname, oname, plen);
        sprintf(nname + plen, "%d", number + 1);
    }
    
        2
  •  0
  •   nos    17 年前

    使用例如。

    int get_rotated_name(char *filename,char *rotatedname,size_t len)
    {
        int version = 0;
        int pos = strrchr(filename,'_');
    
        if(pos == -1)
          return 1;
        if(isdigit(filename[pos+1])) {
          version = atoi(&filename[pos+1];
          filename[pos] = 0;
       }
    
       version++;
       snprintf(rotatedname,len,"%s_%d",filename,version);
    
      return 0;
    }
    
        3
  •  0
  •   nik    17 年前

    strtok

    • strncmp 确认它,或使用 sscanf
    • atoi sscanf %d
    • sprintf 使用新整数

    而且,如果你不需要像函数建议的那样从之前传递的文件名中实际生成下一个文件名,只需保留一个常量字符串和一个旋转整数,该整数与 生成下一个文件名。

        4
  •  0
  •   harishvk27    17 年前

    您真的要同时旋转序列号吗?。

        5
  •  0
  •   Osama Al-Maadeed    17 年前

    • '文件名_9'=> '9'

    我将原始字符串取p,将n+1转换为字符串并将其连接起来。

    • '文件名-3786'=>'文件名-3787'

    您还可以在第p个字符处查找分隔符,如“”、“_”、“-”,并将其删除,然后始终添加一个固定的分隔符,例如_。

        6
  •  0
  •   eniacz    8 年前

    有什么std库可以做到这一点吗?

    https://linux.die.net/man/8/logrotate )而不是重新发明轮子。