By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,125 Members | 1,249 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,125 IT Pros & Developers. It's quick & easy.

Saving to a file

P: n/a
Hi,

I am trying to save information onto a file, but I have to have it print on
the screen first and then I would like to save it into the file. The problem
is that the information that I print on the screen is a bit different than
what is saved onto the file, but when I try to save it into a file, it saves
what is printed on the screen and not what I have in my SaveFile routine.
Here is an example of what the problem is in my program:

main()
{

cout << endl << "Line Number " << gnLineNumber << " Part Number " <<
gnPartNumber;

SaveFile();
stream=freopen("CON", "w", stdout);

}

SaveFile()
{

if((stream = freopen("aOutput.cpp", "a", stdout)) == NULL) exit(-1);

cout << endl << " Model Number " << gnModelNumber << " Line Number " <<
gnLineNumber << " Part Number " << gnPartNumber;
}

Instead of it printing what is in the SaveFile routine, it prints what is in
the main program in my aOutput file. What am I doing wrong?

TIA
Roy
Jul 23 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a
Are you sure you get what is in the main program in your aOutput file?

I tried it with the following codes and what I got in my aOutput is
what is in the Savefile routine:
---------------------------------------------------------
FILE * stream;
int gnModelNumber = 300;
int gnLineNumber = 400;
int gnPartNumber = 500;
void SaveFile()
{
if((stream = freopen("aOutput.cpp", "a", stdout)) == NULL) exit(-1);

cout << endl
<< " Model Number "
<< gnModelNumber
<< " Line Number "
<< gnLineNumber
<< " Part Number "
<< gnPartNumber;
}

int main(int argc, char* argv[])
{
cout << endl
<< "Line Number "
<< gnLineNumber
<< " Part Number "
<< gnPartNumber;
SaveFile();
stream=freopen("CON", "w", stdout);

return 0;
}
--------------------------------------------------------------------------------

Aries Sun

Jul 23 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.