Saving to a file

R

Roy Gourgi

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
 
S

sun.aries

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;
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,578
Members
45,052
Latest member
LucyCarper

Latest Threads

Top