如何在c++中删除文件

3lxsmp7m  于 2023-04-08  发布在  其他
关注(0)|答案(5)|浏览(235)

我做了一个函数,首先读取文件并检查它是否存在,然后在确认后将其删除,但如果我直接喜欢

remove("a.text");

它会删除名为a.txt的文件,但当我使用函数

int deletediary()
{
    string searchfilename;
    cout<<"\nPlease enter the filename to be searched\n";
    cin>>searchfilename;
    searchfilename.append(".txt");
    fileread.open(searchfilename.c_str());
    if(!fileread){
        cout<<"\nERROR :Either you didn't enter an invalid date or you entered an date with no entry\n\n";
        A :
        cout<<"Continue to search? (y/n)\n";
        if(getch()=='y')
        {
            modifydiary();
        }
        else if(getch()=='n')
        {
            menuview();
        }
        else 
        {
            cout<<"Enter Correct Option\n";
            goto A;
        }
    }
    else{
        system("cls");
        int i;
        B :
        cout<<"Are you sure you want to remove this diary entry? (y/n)";
        if(getch()=='y')
        {
            remove(searchfilename.c_str());
        }
        else if(getch()=='n')
        {
            menuview();
        }
        else 
        {
            cout<<"Enter Correct Option\n";
            goto B;
        }   
        cout<<"INFO :Deleted!!!";
        system("pause");
        menuview();
    }

它只检查文件名但不删除它。

3mpgtkmj

3mpgtkmj1#

c++17中,我们有filesystem库,它提供了轻松处理问题的工具。
示例:

#include <filesystem>
#include <iostream>
#include <string>

int main()
{
  std::string searchfilename;
  std::cout << "Please enter the filename to be searched\n";
  std::cin >> searchfilename;
  try {
    if (std::filesystem::remove(searchfilename))
       std::cout << "file " << searchfilename << " deleted.\n";
    else
       std::cout << "file " << searchfilename << " not found.\n";
  }
  catch(const std::filesystem::filesystem_error& err) {
     std::cout << "filesystem error: " << err.what() << '\n';
  }
}
yfwxisqw

yfwxisqw2#

您忘记关闭已打开的文件。因此,关闭文件,它应该可以工作。
注:该解决方案适用于@AkhileshSharma,并将评论作为答案,以回答问题。

camsedfj

camsedfj3#

当你尝试删除一个文件时,你应该立即处理remove函数的返回值。如果成功,它返回0,如果失败,它返回非零值。

const int result = remove( "no-file" );
if( result == 0 ){
    printf( "success\n" );
} else {
    printf( "%s\n", strerror( errno ) ); // No such file or directory
}

removestdio.h文件中
strerrorstring.h
因此,在执行remove函数之后,请检查它没有被删除的原因。
错误号存储在errno变量中,strerror可以将错误号Map到告诉失败原因的字符串。
你也可以使用perror命令测试错误代码和Linux终端

> perror 0
OS error code   0:  Success
> perror 1
OS error code   1:  Operation not permitted
> perror 2
OS error code   2:  No such file or directory
> perror 3
OS error code   3:  No such process
> perror 4
OS error code   4:  Interrupted system call
> perror 5
OS error code   5:  Input/output error
gz5pxeao

gz5pxeao4#

//you have to give the path of the file for example(path="C:\\data\\newfolder")
       System::IO::DirectoryInfo^  directory = gcnew System::IO::DirectoryInfo(path);
       for each(System::IO::FileInfo^ file in directory->GetFiles())
       {
       //for getting the extension of file(if you want to delete specific extension file)
       String^ s = file->Extension;
       if (file->Extension == ".png")
       {
        file->Delete();
       }
       }
zbdgwd5y

zbdgwd5y5#

下面是删除文件另一种简单方法

//complete location of file with path
std::string fileLocStr;
fileLocStr.append("C:\temp\test.txt");

//create dos command to delete file
std::string delFileCmd = "del /s /q " + fileLocStr;
cout << "delFileCmd: " << delFileCmd << endl;

//this executes the command
int delFileStatus = system(delFileCmd.c_str());

相关问题