我是C新手,並且在使用chdir()時遇到問題。我使用一個函數來獲取用戶輸入,然後從中創建一個文件夾並嘗試chdir()進入該文件夾並創建另外兩個文件。無論何時我嘗試通過查找器訪問文件夾(手動)我沒有權限。無論如何,這裏是我的代碼,任何提示?在C中更改工作目錄?
int newdata(void){
//Declaring File Pointers
FILE*passwordFile;
FILE*usernameFile;
//Variables for
char accountType[MAX_LENGTH];
char username[MAX_LENGTH];
char password[MAX_LENGTH];
//Getting data
printf("\nAccount Type: ");
scanf("%s", accountType);
printf("\nUsername: ");
scanf("%s", username);
printf("\nPassword: ");
scanf("%s", password);
//Writing data to files and corresponding directories
umask(0022);
mkdir(accountType); //Makes directory for account
printf("%d\n", *accountType);
int chdir(char *accountType);
if (chdir == 0){
printf("Directory changed successfully.\n");
}else{
printf("Could not change directory.\n");
}
//Writing password to file
passwordFile = fopen("password.txt", "w+");
fputs(password, passwordFile);
printf("Password Saved \n");
fclose(passwordFile);
//Writing username to file
usernameFile = fopen("username.txt", "w+");
fputs(password, usernameFile);
printf("Password Saved \n");
fclose(usernameFile);
return 0;
}
這行很奇怪:'int chdir(char * accountType);' – lbonn