如何从文件夹中删除文件

库尔特·雅各布·乌尔基科

我正在为其中包含fstream的学校项目工作,但问题是我无法删除另一个文件夹中的特定文件,这是代码:

            cout << "(Enter Y to delete, any key to not delete)";
            cin >> deleteAns;

            if(deleteAns == "Y" || deleteAns == "y"){
                //delete book
                cout << endl;
                
                string path = "./Database/" + convertToUnderScore(deleteSearch) + ".dat";
                cout << path << endl;//to print the pathname
                if(remove(path.c_str()) != 0){
                    cout << "There's an error in deleting the file.";
                }else{
                    cout << "File Deleted!";
                }

                backToMenu();
            }else{
                cout << endl;
                center(windowSize/2, " ", "Have a Nice Day!");
                backToMenu();
            }

该文件位于数据库文件夹内。

胡安卡·多明格斯(Juanca Dominguez)

您确定路径字符串正确吗?另一个选择是,如果您以前打开过该文件,则取决于您的操作系统:

...此功能的行为是实现定义的(特别是POSIX系统取消链接文件名,尽管直到最后一个运行的进程关闭文件后,文件系统空间才被回收,即使这是到文件的最后一次硬链接,Windows不允许删除文件)“

参考:https//en.cppreference.com/w/cpp/io/c/remove

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

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

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章