【发布时间】:2015-12-29 19:00:54
【问题描述】:
#include<iostream>
#include<fstream>
#include<string>
#include<vector>
using namespace std;
int main(){
ofstream out;
ifstream in;
out.open("The Necessary Death of Charlie Countryman2.srt");
if (out.fail()) {
perror("The Necessary Death of Charlie Countryman2.srt");
}
in.open("The Necessary Death of Charlie Countryman.srt");
if (in.fail()) {
perror("The Necessary Death of Charlie Countryman.srt");
}
vector<string> input;
string inc;
while (getline(in, inc)) {
input.push_back(inc);
}
for (int k = 0; k < input.size(); k++) {
out << input[k] << endl;
}
return 0;
}
当我使用 ifstream in 从 somefile.txt 读取并使用 写入 anotherfile.txt >ofstream in,一切正常,但假设我注释掉 out.open("anotherfile.txt") 并执行代码,没有错误,当我打开 anotherfile.txt它是空的。
我的问题是在
for (int k = 0; k < input.size(); k++) {
out << input[k] << endl;
}
会发生什么?
input[k] 发送到哪里或发送到什么位置?如果是 cout,它会转到命令提示符,如果 out.open("anotherfile.txt") 没有被注释,那么它会转到 anotherfile.txt。
【问题讨论】:
-
似乎它被发送到一个从未使用过的缓冲区。但我找不到有关它的更多信息
-
当一个输出文件没有被打开时,文本被简单地加载到一个缓冲区中,并在程序终止时在其中销毁。除非您打开文件进行阅读,否则您尝试发送到输出文件的文本将被转发回堆栈。
-
@I3ck 和@Poriferous 谢谢你们,这就是我想知道我的
input[k]发送到哪里。