简体   繁体   English

在文件 class 析构函数(C++)中关闭文件之前刷新和同步

[英]Flush and sync before closing file in File class destructor (C++)

As an exercise I want to write a class which describes a file containing blocks of data.作为练习,我想编写一个 class 来描述一个包含数据块的文件。 Inside the destructor of the class I simply called close , since (quoting from http://www.cplusplus.com/doc/tutorial/files/ )在 class 的析构函数内部,我简单地调用了close ,因为(引用自http://www.cplusplus.com/doc/tutorial/files/

When the buffer is flushed, all the data contained in it is written to the physical medium (if it is an output stream).当缓冲区被刷新时,其中包含的所有数据都会写入物理介质(如果是 output 流)。 This process is called synchronization and takes place under any of the following circumstances: When the file is closed: before closing a file, all buffers that have not yet been flushed are synchronized and all pending data is written or read to the physical medium.这个过程称为同步,在以下任何一种情况下都会发生: 文件关闭时:在关闭文件之前,所有尚未刷新的缓冲区都被同步,所有待处理的数据都被写入或读取到物理介质。

A sample code is given below.下面给出了一个示例代码。

#pragma once
#include <filesystem>
#include <fstream>
#include <string>

namespace fs = std::filesystem;

template<class Block>
class BlockFile {
  
private:

  fs::path mFilePath;
  std::fstream mFile;
  
public:

  BlockFile(fs::path fPath) : mFilePath{fPath}
  {
    // this is so wrong for so many reasons (file might exist, not thread safe etc.) but anyway
    auto touch = ofstream{mFilePath};
  }

  ~BlockFile() {
    mFile.close();
  }  

// read, write, append methods etc..

};

However, when I read another solution, the destructor was actually implemented as但是,当我阅读另一个解决方案时,析构函数实际上实现为

~BlockFile() 
{
  mFile.flush();
  mFile.sync();
  mFile.close();
}

My main question is why do we need to explicitly flush and sync, since the quote above says that it is done before the file is closed?我的主要问题是为什么我们需要显式刷新和同步,因为上面的引用说它是在文件关闭之前完成的?

Extra question: In the accepted answer of this question What is the difference between flush() and sync() in regard to fstream buffers?额外的问题:在这个问题的公认答案中,flush() 和 sync() 在 fstream 缓冲区方面有什么区别? it is mentioned提到了

basic_ostream::flush This is a non-virtual function which writes uncommited changes to the underlying buffer. basic_ostream::flush 这是一个非虚拟 function,它将未提交的更改写入底层缓冲区。 In case of error, it sets an error flag in the used stream object.如果出现错误,它会在使用的 ZF7B44CFFAFD5C52223D5498196C8A2E7BZ object 中设置错误标志。 This is because the return value is a reference to the stream itself, to allow chaining.这是因为返回值是对 stream 本身的引用,以允许链接。

basic_filebuf::sync This is a virtual function which writes all pending changes to the underlying file and returns an error code to signal success or failure. basic_filebuf::sync 这是一个虚拟 function,它将所有挂起的更改写入底层文件并返回错误代码以指示成功或失败。

What is the difference between uncommitted changes and pending changes ?未提交的更改挂起的更改有什么区别?

My main question is why do we need to explicitly flush and sync, since the quote above says that it is done before the file is closed?我的主要问题是为什么我们需要显式刷新和同步,因为上面的引用说它是在文件关闭之前完成的?

You don't.你没有。 Not all code found on the internet is of highest quality.并非所有在互联网上找到的代码都是最高质量的。 :-) :-)

  ~BlockFile() {
    mFile.close();
  }  

This is not really necessary either, as the fstream destructor also calls close if the file is still open.这也不是必需的,因为如果文件仍然打开,fstream 析构函数也会调用 close。

The "only" reason to call close is to check its return value, which indicates if it was successful or not.调用 close 的“唯一”原因是检查它的返回值,它表明它是否成功。 Perhaps flushing the buffers caused a disk full?也许刷新缓冲区会导致磁盘满? Or writing to a network store failed because the connection was lost?或者由于连接丢失而写入网络存储失败?

What is the difference between uncommitted changes and pending changes?未提交的更改和挂起的更改有什么区别?

Not much, I think they are just synonyms here.不多,我认为它们在这里只是同义词。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM