我試圖刪除一個文件,然後從一個沒有工作的函數中刪除文件的名稱中重命名臨時文件。請幫我c中的remove()在函數中不起作用
boolean delete_user(char user_name[256]) //only for Admin
{
boolean status = FALSE;//what does the function return
User_Data *ud = NULL;
boolean found_user = FALSE;
FILE *new_file = NULL;
FILE *fp = NULL;
char user_name_to_copy[256];
char password_to_copy[256];
char old_file_name[256];
ud = find_user(user_name);
if (ud == NULL) {
printf("The username wasn't found!\n");
return FALSE;
}
if (!strcmp(ud->permission_type, "Admin")) {
printf("Cant delete an admin.");
return FALSE;
} else {
// the user to delete was found
new_file = fopen("duplicate.txt", "wt");
strcpy(old_file_name, ud->permission_type);
strcat(old_file_name, "s.txt"); //the name of the file is in plural and ends with .txt
fp = fopen(old_file_name, "rt");
while (!feof(fp)) {
//copy all the users except the user to delete the new file
fscanf(fp, "%s %s\n", user_name_to_copy, password_to_copy);
if (strcmp(user_name_to_copy, user_name)) {
fprintf(new_file, "%s %s\n", user_name_to_copy, password_to_copy);
}
}
fclose(fp);
fclose(new_file);
printf(" %d ", remove(old_file_name));
rename("duplicate.txt", old_file_name);
remove("duplicate.txt");
return TRUE;
}
}
這個函數不起作用,當我從另一個函數調用它,但從主函數工作得很好。
你是否已經通過調試器直通了代碼? – Dai
向我們展示如何從** main **調用函數,以及如何從**另一個函數調用** – iwaduarte
請參閱['while(!feof(file))'總是錯誤的](http:// stackoverflow .com/questions/5431941/while-feof-file-is-always-wrong)。這不太可能是你報告的主要問題的原因,但即使他們還沒有引起你的痛苦,你也不應該沉迷於壞習慣。 –