首先,我使用的是 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/