Removing Characters from a File

2020-04-17 03:16发布

How can arbitrary characters be removed (not replaced by something) in a file?

#include <fstream>

int main()
{
    std::fstream FileStream("MyFile.txt", ios_base::in | ios_base::out | ios_base::binary);
    // For the sake of argument, MyFile.txt already has stuff in it.

    FileStream.seekg(5);
    FileStream.remove(); // Something like this.
}

1条回答
贼婆χ
2楼-- · 2020-04-17 03:56

You have two options:

  1. Read the entire file in the memory, then save it to the file excluding unwanted parts.
  2. Copy the source file to newly created file excluding unwanted parts, remove the source file, then rename second file.
查看更多
登录 后发表回答