I’m trying to read from files created outside of the program, but am having some trouble. The program has the user create a file. Then it reads words from two .txt files created outside of the program, and then writes the words to the created file.
#include "std_lib_facilities.h"
int main()
{
string word;
cout << "Create file.\n";
char name[20];
cin >> name;
ofstream ost(name, ios::out);
cout << "Open first file.\n";
char filename[20];
cin >> filename;
ifstream ist(filename);
while(ist >> word) ost << word << " ";
ist.close();
cout << "Open second file.\n";
cin >> filename;
ifstream isttwo(filename);
while(isttwo >> word) ost << word << " ";
isttwo.close();
ost.close();
keep_window_open();
}
However, when I open the created file in notepad, it comes out blank. Is this because reading into a string is impossible because the files being read were created separately? I’m not really sure. Any help is appreciated.
The code is correct. Just make sure when you write the name of the first file and the second one you write their extensions as well.
For example :