21xrx.com
2024-05-20 10:49:13 Monday
登录
文章检索 我的文章 写文章
C++中删除指定内容的二进制文件
2023-07-13 10:29:43 深夜i     --     --
C++ 删除 指定内容 二进制文件

在C++编程中,有时候需要删除指定的二进制文件。这种需求可能出现在我们需要定期清理过期文件的时候。下面介绍一种简单高效的方法。

首先,C++中可以使用remove()函数来删除二进制文件。这个函数需要指定被删除文件的路径,并且不能删除非空目录。因此,我们需要先判断文件是否存在,如果存在则删除,否则忽略。代码如下:


#include <iostream>

#include <fstream>

using namespace std;

bool fileExist(const string& fileName){

  ifstream infile(fileName.c_str());

  return infile.good();

}

void deleteFile(const string& fileName){

  if(fileExist(fileName)){

    if(remove(fileName.c_str()))

      cout<<"File deleted successfully."<<endl;

    

    else

      cout<<"Error deleting file."<<endl;

    

  }else

    cout<<"File not found."<<endl;

  

}

int main(){

  string fileName = "data.bin";

  deleteFile(fileName);

  return 0;

}

上述代码中,我们定义了一个fileExist()函数来判断文件是否存在。如果文件存在,则调用remove()函数来删除文件;否则输出"File not found."的提示信息。

上述代码可以轻松地删除任意二进制文件。但是,如果需要仅删除文件中包含指定内容的数据块,该怎么做呢?例如,我们需要删除二进制文件中前4个字节为0x12345678的数据块。解决方法如下:


#include <iostream>

#include <fstream>

#include <cstring>

using namespace std;

bool fileExist(const string& fileName){

  ifstream infile(fileName.c_str());

  return infile.good();

}

void deleteDataBlock(const string& fileName, const char data[], const int dataSize){

  if(fileExist(fileName)){

    fstream file;

    file.open(fileName.c_str(), ios::in | ios::binary);

    if(file){

      long fileSize = 0;

      file.seekg (0, ios::end);

      fileSize = file.tellg();

      

      char *buffer = new char[fileSize];

      file.seekg (0, ios::beg);

      file.read(buffer, fileSize);

      file.close();

      

      size_t start = 0;

      size_t end = fileSize - 1;

      bool found = false;

      for(size_t i = 0; i < fileSize - dataSize; i++){

        if(memcmp(buffer + i, data, dataSize) == 0){

          start = i;

          end = start + dataSize - 1;

          found = true;

          break;

        }

      }

      

      if(found){

        file.open(fileName.c_str(), ios::out | ios::binary);

        if(file){

          if(start != 0){

            file.write(buffer, start);

          }

          if(end < fileSize - 1){

            file.write(buffer + end + 1, fileSize - end - 1);

          }

          file.close();

          cout<<"Data block deleted successfully."<<endl;

        }

        else

          cout<<"Error opening file."<<endl;

        

      }

      else

        cout<<"Data block not found."<<endl;

      

      delete[] buffer;

    }

    else

      cout<<"Error opening file."<<endl;

    

  }else

    cout<<"File not found."<<endl;

  

}

int main(){

  string fileName = "data.bin";

  char data[] = 0x12;

  int dataSize = sizeof(data)/sizeof(char);

  deleteDataBlock(fileName, data, dataSize);

  return 0;

}

上述代码中,我们增加了一个deleteDataBlock()函数来删除文件中指定的数据块。该函数中使用了文件指针,在内存中读取二进制文件,查找指定数据块,并删除之后重新写入文件。

这种方法可以在不影响文件其他部分的前提下删除指定的数据块,同时内存中仅读取了一次文件,代码效率比较高。当然,如果想要删除多个不同的数据块,可以把deleteDataBlock()函数封装为一个循环结构,在主函数中反复调用。

  
  

评论区

{{item['qq_nickname']}}
()
回复
回复