expand file names that have environment variables in their path

后端 未结 8 2113
礼貌的吻别
礼貌的吻别 2020-12-01 08:06

What\'s the best way to expand

${MyPath}/filename.txt to /home/user/filename.txt

or

%MyPath%/filename.txt to c:\\Document         


        
8条回答
  •  误落风尘
    2020-12-01 08:32

    Within the C/C++ language, here is what I do to resolve environmental variables under Unix. The fs_parm pointer would contain the filespec (or text) of possible environmental variables to be expanded. The space that wrkSpc points to must be MAX_PATH+60 chars long. The double quotes in the echo string are to prevent the wild cards from being processed. Most default shells should be able to handle this.

    
       FILE *fp1;
    
       sprintf(wrkSpc, "echo \"%s\" 2>/dev/null", fs_parm);
       if ((fp1 = popen(wrkSpc, "r")) == NULL || /* do echo cmd     */
           fgets(wrkSpc, MAX_NAME, fp1) == NULL)/* Get echo results */
       {                        /* open/get pipe failed             */
         pclose(fp1);           /* close pipe                       */
         return (P_ERROR);      /* pipe function failed             */
       }
       pclose(fp1);             /* close pipe                       */
       wrkSpc[strlen(wrkSpc)-1] = '\0';/* remove newline            */
    
    

    For MS Windows, use the ExpandEnvironmentStrings() function.

提交回复
热议问题