c - c 中的 remove() 在函数中不起作用

标签 c

我尝试删除一个文件,然后从一个不起作用的函数中以已删除文件的名称重命名临时文件。请帮助我

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;
    }
}

当我从另一个函数调用它时,这个函数不起作用,但从 main 函数调用它时,它工作得很好。

最佳答案

您的代码中存在多个问题:

  • 您没有检查 fopen() 是否成功打开了文件。

  • 在连接字符串以计算权限文件时,您无法防止潜在的缓冲区溢出。

  • 您应该将数组大小传递给 fscanf 以防止潜在的缓冲区溢出。

  • 您应该使用 strerror(errno) 输出有意义且信息丰富的错误消息,以及失败的原因。

  • 复制权限文件的循环不正确:Why is “while ( !feof (file) )” always wrong?

  • 即使重命名失败但删除了重复文件,但设法删除了原始文件:两个文件都可能丢失。相反,如果重命名操作成功,删除重复文件是多余的。

以下是您可以如何改进代码:

#include <errno.h>
#include <string.h>

boolean delete_user(char user_name[256]) {  //only for Admin
    boolean status = FALSE; // the function return value
    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("User '%s' was not found!\n", user_name);
        return FALSE;
    }
    if (!strcmp(ud->permission_type, "Admin")) {
        printf("Cannot delete user '%s', user has admin status.\n", user_name);
        return FALSE;
    }
    // the user to delete was found 
    new_file = fopen("duplicate.txt", "wt");
    if (new_file == NULL) {
        printf("Cannot open file 'duplicate.txt': %s\n"
               strerror(errno));
        return FALSE;
    }
    // the name of the file is in plural and ends with .txt
    snprintf(old_file_name, sizeof old_file_name, "%ss.txt", ud->permission_type);
    fp = fopen(old_file_name, "rt");
    if (fp == NULL) {
        printf("Cannot open user file '%s': %s\n"
               old_file_name, strerror(errno));
        return FALSE;
    }
    // copy all the users except the user to delete the new file 
    while (fscanf(fp, "%255s %255s\n", user_name_to_copy, password_to_copy) == 2) {
        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);
    if (remove(old_file_name)) {
        printf("Error removing file '%s': %s\n",
               old_file_name, strerror(errno));
        remove("duplicate.txt");
        return FALSE;
    }
    if (rename("duplicate.txt", old_file_name)) {
        printf("Error renaming file 'duplicate.txt' to '%s': %s\n",
                old_file_name, strerror(errno));
        // keep duplicate.txt
        return FALSE;
    }
    // duplicates.txt was successfully renamed, no need to remove it.
    return TRUE;
}

注意事项:

  • 您在当前目录中创建临时文件,该文件可能位于与权限文件不同的驱动器上。 rename() 可能无法将重复文件从当前目录移动到该目录。如果这是失败的原因,应该运行代码并进行适当的诊断。

关于c - c 中的 remove() 在函数中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41918268/

相关文章:

c - ((float) rand()/(float)((1 << 31) - 1)) 的含义

复制二进制文件的内容

c - 如何组织C多文件项目?

c - C中获取信号发送者的PID

c - 如何为函数中的指针分配内存

c - 如何在c中没有for循环的情况下将用户输入存储在数组中

c - 制作命名管道并使用轮询

c - 'write' 函数的正确缓冲区大小是多少?

c - 为什么我会出现段错误?

c++ - 输出缓冲区何时刷新?