Click to See Complete Forum and Search --> : writing to screen AND disk:


Mansoor8
December 9th, 2004, 10:16 AM
Hello:
I am researching the code to write to both screen and to hard drive (file) simultaneously, if this is possible; I am aware that I can program cin to write to file on disk, but it will not appear on the screen (?). If I am taking input from a user and I wish to copy it, why can I not write to disk while it is printed to console before them also? I am unsure about this and I hope for some knowledgable insight into this operation.

Thank-you in advance for your assistance,
Matthew Mansoor

Marc G
December 9th, 2004, 10:41 AM
Open an ifstream to the file you want to write to and output both to the screen and file.
#include <fstream>
#include <string>
using namespace std;
...
ifstream ifs;
ifs.open("somefile.txt", ios_base::out);
if (ifs.is_open())
{
string str;
// Get input from user in str

// Now, output to screen
cout << str;
// and output to file
ifs << str;

...

ifs.close();
}

Mansoor8
December 9th, 2004, 10:45 AM
Marc:
Cool :p Thank-you very much. I will try this today. Excellent. ;)

Matthew Mansoor