문자열을 chdir()에 전달하려고합니다. 하지만 난 항상 몇 가지 후행 물건 chdir() 실패하게 보인다.C에서 문자열 파싱
#define IN_LEN 128
int main(int argc, char** argv) {
int counter;
char command[IN_LEN];
char** tokens = (char**) malloc(sizeof(char)*IN_LEN);
size_t path_len; char path[IN_LEN];
...
fgets(command, IN_LEN, stdin)
counter = 0;
tmp = strtok(command, delim);
while(tmp != NULL) {
*(tokens+counter) = tmp;
tmp = strtok(NULL, delim);
counter++;
}
if(strncmp(*tokens, cd_command, strlen(cd_command)) == 0) {
path_len = strlen(*(tokens+1));
strncpy(path, *(tokens+1), path_len-1);
// this is where I try to remove the trailing junk...
// but it doesn't work on a second system
if(chdir(path) < 0) {
error_string = strerror(errno);
fprintf(stderr, "path: %s\n%s\n", path, error_string);
}
// just to check if the chdir worked
char buffer[1000];
printf("%s\n", getcwd(buffer, 1000));
}
return 0;
}
더 좋은 방법이 있어야합니다. 어떤 도움을 줄 수 있습니까? I'vr은 scanf를 사용하려고했지만 프로그램이 scanf를 호출하면 멈 춥니 다. 당신이를 strncpy를 호출 한 후 경로 문자열에 널 (null) '\ 0'을 추가 할 잊어 버린 것 같습니다
감사
아 예. 어리석은 실수 야. 고마워. 나는 꽤 좌절하고 있었다. – devin
내가 바보 프로그래밍 실수를 할 때마다 니켈을 가지고 있다면 ... –