文字列を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
毎回ニッケルがあったら、愚かなプログラミングミスをしました... –