c++ - 如果您从终端终止程序,打开的 ofstream 对象会发生什么?

标签 c++

首先,我使用的是 Shell 终端,所以我通过 Ctrl-C 来终止程序。假设我有这段代码:

#include <iostream>
#include <fstream>
using namespace std;

int main (){

  ofstream ofs("testing.txt");

  for (unsigned int i=0;;i++){
    ofs << i << endl;
  }
}

此程序会将每个自然数保存在一个文件中,直到我将其终止。我的问题是:当程序被杀死时,fstream 的析构函数被调用,或者我会有一个损坏的文件或类似的东西?请记住,我将来会再次使用该文件。

最佳答案

嗯,你使用中断(Ctrl-C),在一般情况下:

...an interrupt is a signal to the processor emitted by hardware or software indicating an event that needs immediate attention. An interrupt alerts the processor to a high-priority condition requiring the interruption of the current code the processor is executing. The processor responds by suspending its current activities, saving its state, and executing a function called an interrupt handler (or an interrupt service routine, ISR) to deal with the event...

你的输出流会发生什么是系统特定的,它取决于是否正在进行一些缓存。

关于c++ - 如果您从终端终止程序,打开的 ofstream 对象会发生什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32522528/

相关文章:

c++ - LNK1120 类错误(工厂模式)。不确定它来自哪里

c++ - 模板元代码和私有(private)成员

c++ - 存储值 Turbo C++

c++ - 从可变参数函数返回格式化字符串

c++ - 将一个类更改为它的子类

c++ - 如何在运行时/动态地在 C++ 中获取类的所有属性/变量

c++ - Linux 中的 Cout 和 Cin - 看不到控制台

c++ - 使用 get 函数访问存储在节点中的数据

c++井字棋程序

c++ - 如何调用带有表类型参数的存储过程