int main(int argc, char **argv)
{
char input[150];
char change[2] = "cd";
char *directory;
while(1) {
prompt();
fgets(input, 150, stdin);
if(strncmp(change, input, 2) == 0) {
directory = strtok(input, " ");
directory = strtok(NULL, " ");
printf(directory);
chdir(directory);
perror(directory);
}
if(feof(stdin) != 0 || input == NULL) {
printf("Auf Bald!\n");
exit(3);
}
}
}
当我启动这个程序并输入“cd test”时,会出现“没有这个文件或目录”的提示。但是确实存在名为“test”的目录。
运行在Arch Linux上。
directory
字符串:printf("directory = [%s]\n", directory);
,以防名称中有多余的换行符或其他不需要/不可见的字符。 - Paul Rstrncmp(...2)
,所以是合法的,尽管你的做法也很好。 - rodrigo