#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的在和寫入使用ofstream的在anotherfile.txt,一切工作正常,但假設我註釋掉out.open("anotherfile.txt")
並執行從somefile.txt閱讀代碼,沒有錯誤,當我打開anotherfile.txt它是空的。當ofstream object.open()被註釋掉時會發生什麼?
我的問題是在
for (int k = 0; k < input.size(); k++) {
out << input[k] << endl;
}
會發生什麼?
哪裏或什麼是input[k]
發送給?如果是cout它會轉到命令提示符,如果out.open("anotherfile.txt")
未被註釋,則轉到anotherfile.txt。
好像它被髮送到從未使用的緩衝區。但是我找不到關於它的更多信息 – I3ck
當輸出文件沒有被打開時,文本被簡單地加載到緩衝區中並在程序終止時被銷燬。除非您打開文件進行閱讀,否則您嘗試發送至輸出文件的文本將被中繼回堆棧。 – Poriferous
@ I3ck和@ Poriferous謝謝你們,這就是我想知道我的'輸入[k]'發送到哪裏。 – nSv23