chdir を使用してプロセスの作業ディレクトリを変更しようとしています。現在のディレクトリにappleディレクトリがあるとします。プロンプトに「apple」と入力すると、返される結果が -1 になるのはなぜですか? apple と入力すると、文字列の最後に '\n' 文字も付けられるためですか? また、変数を使用して格納できる場合、ディレクトリを変更する意味は何ですか?
#include<stdio.h>
#include<string.h>
#include<errno.h>
#include<unistd.h>
int main(void){
char path[256];
fgets(path, 256, stdin);
printf("%s", path);
int result = chdir(path);
if(result != 0){
printf("%d\n", result);
}
}