문제

INGERACODICETAG 코드가 잘못된 경로를 반환하는 이유는 다음과 같습니다.

및 해결 방법이므로 두 경우 모두 작동합니다.

#include <stdio.h>
#include <stdlib.h>
#include <getopt.h>

int main(int argc, char *argv[]) {
    int opt = 0;
    int long_index = 0;
    char *f; 
    static struct option longopt[] = { 
        {"foo", required_argument, 0,  'd' },
        {0,0,0,0}
    };  
    while ((opt = getopt_long(argc, argv,"d:", longopt, &long_index )) != -1) {
        switch (opt) {
            case 'd' : 
                printf("\n%s\n", optarg);
                f = realpath (optarg, NULL);
                if (f) printf("%s\n", f); 
                break;
            default: 
                exit(1);
        }   
    }   
    return 0;
}
.

출력 :

$ ./a.out --foo=~/.bashrc
  ~/.bashrc

$ ./a.out --foo ~/.bashrc
  /home/user/.bashrc
.

도움이 되었습니까?

해결책

쉘에 의해 "물결표 확장"이 수행되기 때문에 일어납니다. 자체적으로 유효한 경로가 아닙니다.tilde ~은 문자열 인수의 시작 부분에있는 경우에만 홈 디렉토리로 확장됩니다. 경로처럼 보입니다.예 :

$ echo ~
/home/sigi
$ echo ~/a
/home/sigi/a
$ echo ~root/a
/root/a
$ echo ~a
~a
$ echo a/~
a/~
.

첫 번째 경우 에도이 기능을 제공하려는 경우 쉘이 셸에서 사용되는 단어 확장을 사용하는 경우에도 쉘이 사용하는 경우에는 필요한 모든 정보를 찾을 수 있습니다.이 참조 .

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top