我的任务是cd在C 语言中对linux 命令进行编程。我认为使用该chdir()方法将是微不足道的,但是我的目录没有更改。有趣的是chdir()的返回状态为0,而不是-1,这意味着chdir()并未失败。这是我正在使用的两种情况chdir():
1。
char *dir = getenv("HOME"); // Here dir equals the home environment.
int ret = chdir(dir);
printf("chdir returned %d.\n", ret);
Run Code Online (Sandbox Code Playgroud)
ret 返回1。
2。
int ret = chdir(dir); // Here dir equals the user's input.
printf("chdir returned %d.\n", ret);
Run Code Online (Sandbox Code Playgroud)
ret 如果目录在我的路径中,则返回1。
我使用chdir()错了吗?我似乎在任何地方都找不到答案。任何帮助将非常感激。