删除功能不起作用

马克思主义

我正在尝试删除C程序中的文件因此,我首先检查文件是否存在,然后检查是否存在删除功能。这是我的代码:

    if (!(f = fopen(position, "r")))
    {
        system("cls");
        printf("User does not exist! Please enter the user again: ");
    }
    else
    {
        status = remove(position);

        /*Check if file has been properly deleted*/
        if(status == 0)
        {
            printf("User deleted successfully.\n\n");
            break;
        }
        else
        {
            printf("Unable to delete the user\n");
        }
    }

当然,如果文件确实存在,则删除文件应该没有问题。无论如何,这段代码无法正常工作。我刚回来"Unable to delete the user"

我也尝试过使用unlink以及导入,unistd.h但是没有运气。

我究竟做错了什么?

夏隆

检查此相关问题

如果您必须删除fopen()-ed路径(文件),则仅当您删除fclose()时才将其删除。**

但是我认为,如果您只是想删除文件而不用之前使用过的文件,请不要使用它,fopen而只需调用该remove函数。

**编辑:甚至未知,它是系统相关的。

因此,删除文件的最佳方法是执行此操作,即在不流式传输文件时调用remove(path_of_file):

remove(path_of_file);

或者如果您需要打开文件:

fopen/open;
(...)
fclose/fclose;
remove

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章