function to split a filepath into path and file

前端 未结 6 1528
猫巷女王i
猫巷女王i 2020-12-11 11:10

Lets say I have a function:

void split_path_file(char** p, char** f, char *pf)
{
    //malloc and set *p to file path, malloc and set *f to file name
    //p         


        
6条回答
  •  一生所求
    2020-12-11 11:46

    int
    stripfilenameandpath (char *path, char *onlypath, char *onlyfilename)
    {
    /*
    documentacao
    
    path = path com path e arquivo
    onlypath = somente o path
    onlyfilename = somente o arquivo sem o path
    
    */
        int ret;
        int i;
        int p;
        char temp[255];
    
        char *fixo;
    #ifdef WIN32
        const int separator = '\\';
    #else
        const int separator = '/';
    #endif
    
        fixo = path;
    
        if (path == NULL)
          {
    
              if (onlypath != NULL)
                {
                    memset (onlypath, 0, 1);
                }
    
              if (onlyfilename != NULL)
                {
                    memset (onlyfilename, 0, 1);
                }
    
              return 1;
          }
    
        ret = strlen (path);
    
        if (!ret)
          {
    
              if (onlypath != NULL)
                {
                    memset (onlypath, 0, 1);
                }
    
              if (onlyfilename != NULL)
                {
                    memset (onlyfilename, 0, 1);
                }
    
              return 0;
          }
    
        for (i = 0; i  -1; i--)
          {
    
              if (temp[i] == separator)
                {
                    temp[i + 1] = 0;
                    break;
                }
              p++;
          }
    
        p = ret - p;
    
        fixo += p + 1;
    
        if (onlypath != NULL)
          {
              strcpy (onlypath, temp);
          }
    
        if (onlyfilename != NULL)
          {
              strcpy (onlyfilename, fixo);
          }
    
        return 0;
    }
    
    

提交回复
热议问题