#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;
}
When I read from somefile.txt using ifstream in and write into anotherfile.txt using ofstream in, everything works fine, but suppose I comment out out.open("anotherfile.txt")
and execute the code, there is no error and when I open anotherfile.txt it is empty.
My question is during
for (int k = 0; k < input.size(); k++) {
out << input[k] << endl;
}
what happens?
Where or to what is input[k]
sent to? If it was cout it goes to command prompt and if out.open("anotherfile.txt")
was not commented then it goes to anotherfile.txt.