Hi.
I have problems when I try to redirect everything that is sent to cout
to a file.
I have one piece of code that works and one that does not work.
The only difference is which headers I use.
What the code does:
* First writes "This is sent to prompt" to prompt.
* Then writes "This is sent to file" to file (test.txt).
* Then writes "This is also sent to prompt" to prompt.
Code that works:
##########################################
#include <iostream>
#include <fstream>
using namespace std;
int main ()
{
cout << "This is sent to prompt" << endl;
ofstream file;
file.open ("test.txt");
streambuf* sbuf = cout.rdbuf();
cout.rdbuf(file.rdbuf());
cout << "This is sent to file" << endl;
cout.rdbuf(sbuf);
cout << "This is also sent to prompt" << endl;
return 0;
}
##########################################
Code that DOES NOT work:
##########################################
#include <iostream.h>
#include <fstream.h>
int main ()
{
cout << "This is sent to prompt" << endl;
ofstream file;
file.open ("test.txt");
streambuf* sbuf = cout.rdbuf();
cout.rdbuf(file.rdbuf()); //ERROR C2660: function does not take 1
parameters
cout << "This is sent to file" << endl;
cout.rdbuf(sbuf); //ERROR C2660: function does not take 1 parameters
cout << "This is also sent to prompt" << endl;
return 0;
}
##########################################
As you can see the only lines that differs are the first 3 lines in
each file.
The error I get is:
andracout.cpp
andracout.cpp(10) : error C2660: 'rdbuf' : function does not take 1
parameters
andracout.cpp(12) : error C2660: 'rdbuf' : function does not take 1
parameters
I compile with:
cl.exe /GX /TP andracout.cpp
cl.exe = Microsoft (R) 32-bit C/C++ Optimizing Compiler Version
12.00.8168 for 80x86
/GX[-] = enable C++ EH (same as /EHsc)
/TP = compile all files as .cpp
Can someone please tell me how to make my code work also with the old
headers.
Now you might ask why I don't just stick with the code that works.
Short explanation why it must work with the old headers:
I can't use the new headers (<iostream> and <fstream>).
I must use the old headers (<iostream.h> and <fstream.h>).
Longer explanation:
The reason why I can't use the new headers (which works in my example)
is that I use a program (ADS from Agilent) that generates all
C++-code. I only give a piece of code to ADS and it will include the
code in some function in a large and complex .cc-file which it will
then compile by automatic, and I can't change the headers it uses.
My problem would be solved if someone could tell me how I can switch
this line in my code:
cout.rdbuf(sbuf);
....to some other piece of code that does the same thing but that does
not use "streambuf* ios::rdbuf ( streambuf* sb );", since it seems
that it is not defined in the .h-files.
I would be very thankful for any information that can help me solve my
problem.
Also, while I have been looking for a solution to my problem I came
across this piece of text that was found in the "basic_ios.h"-file
under my Cygwin-directory. I don't know if this is relevant for me,
but I strongly sense that it is related to my problem:
From "C:\cygwin\usr\include\c++\3.3.1\bits\basic_ios.h" :
#########################################
/**
* @brief Accessing the underlying buffer.
* @return The current stream buffer.
*
* This does not change the state of the stream.
*/
basic_streambuf<_CharT, _Traits>*
rdbuf() const
{ return _M_streambuf; }
/**
* @brief Changing the underlying buffer.
* @param sb The new stream buffer.
* @return The previous stream buffer.
*
* Associates a new buffer with the current stream, and clears
the
* error state.
*
* Due to historical accidents which the LWG refuses to
correct, the
* I/O library suffers from a design error: this function is
hidden
* in derived classes by overrides of the zero-argument @c
rdbuf(),
* which is non-virtual for hysterical raisins. As a result,
you
* must use explicit qualifications to access this function via
any
* derived class.
*/
basic_streambuf<_CharT, _Traits>*
rdbuf(basic_streambuf<_CharT, _Traits>* __sb);
#########################################
Can anyone see why I get the error I get and how I can avoid it and
still be using the ".h-headers".
Best Regards
/Omid