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”我得到“没有这样的文件或目录”。但是有目录“测试”。chdir() - 没有这样的文件或目录
在Arch Linux上运行。
检查你的''目录'字符串是这样的:'printf(“directory = [%s] \ n”,directory);'如果你的名字中有一个杂散的换行符或其他不想要/不可见的字符。 –
'char change [2] =“cd”'将大小为3的数组赋值给大小为2的变量。请改用char change [] =“cd”'。 –
@ WilliamPursell-实际上,他正在使用'strncmp(... 2)',所以它是合法的,尽管无论如何你都是一个好习惯。 – rodrigo