Re: Redirect COUT to file

From: Omid Rouhani (minskrappost_at_hotmail.com)
Date: 07/21/04


Date: 21 Jul 2004 11:32:22 -0700

tom_usenet <tom_usenet@hotmail.com> wrote in message news:<6efqf0d8qvind4aaq4q451lu4gtu8hsrfi@4ax.com>...
> On 20 Jul 2004 07:26:10 -0700, n_o_sp_a_m@hotmail.com (Omid) wrote:
>

> >cl.exe = Microsoft (R) 32-bit C/C++ Optimizing Compiler Version
> >12.00.8168 for 80x86
>
> What version is that in English? VC 6?

Yes, that's correct. It's VC 6.0.
I should have mentioned that.

> >Can someone please tell me how to make my code work also with the old
> >headers.
>
> Which old headers? The old headers are non-standard, and differ in
> meaning depending upon the exact compiler you are using. I'll assume
> you are using Microsoft's old headers.
>
> In that case, you can't write code that works both for the old and new
> headers, but here is some code that I suspect will work with the old
> headers. Some old iostreams libraries had a class ostream_with_assign,
> and if yours is one of them, this is what you need (untested since I
> don't have any compilers that are old enough to compile it):
>
> #include <iostream.h>
> #include <fstream.h>
>
> int main ()
> {
> cout << "This is sent to prompt" << endl;
> ofstream file;
> file.open ("test.txt");
> ostream_with_assign oldcout = cout;
> cout = file;
> cout << "This is sent to file" << endl;
> cout = oldcout;
> cout << "This is also sent to prompt" << endl;
> return 0;
> }

Thanks a lot for your help. This was what I was looking for!
However, your "ostream_with_assign" should be changed to
"ostream_withassign", otherwise it's all correct and I'm able to
compile and link this file. So far everything is fine, but when I
execute the exe-file the program crashes and I get this error message:

The instruction at "0x00340905" referenced memory at "0x006811e8". The
memory could not be "written".
(The error is shown in the "standard Windows error window" with an ok
and cancel box.)

The program output:s "This is sent to prompt" and the test.txt is
created with the content "This is sent to file".
But the last message "This is also sent to prompt" isn't sent to
STDOUT.

Does anyone have any idea why this happens?
The compilation and linking is successful,
but during the execution I get this error.

I have also tried several modification of the code above.
One of them that is shown below can successfully be executed with
VC++,
but when I tried to compile exactly the same program with Cygwin g++,
it does not compile.
I'm only using standard headers now.
Why is it possible that it works with VC but not with G++?
Code:

//WORKS FINE WITH CL.EXE (VC++ 6.0)
//BUT NOT WITH CYGWIN G++
//################################
#include <iostream>
#include <fstream>

using namespace std;

class ostream_withassign : public ostream {
 public:
  ostream_withassign() : ostream((streambuf*)0) {}
  ~ostream_withassign() {}

  ostream_withassign& operator=(ostream& __s) {
    ios::init(__s.rdbuf());
    return *this;
  }
  ostream_withassign& operator=(streambuf* __s) {
    ios::init(__s);
    return *this;
  }
};

 int main ()
 {
   cout << "This is sent to prompt" << endl;
   ofstream file;
   file.open ("test.txt");
   ostream_withassign oldcout; //Split into 2 lines
   oldcout= cout; //Split into 2 lines
   cout = file;
   cout << "This is sent to file" << endl;
   cout = oldcout;
   cout << "This is also sent to prompt" << endl;
   return 0;
 }
//################################

Note that I now use the standard header files (without .h).

Error using Cygwin g++ (3.3.1):
################
$ g++ mycout.cpp
/usr/include/c++/3.3.1/bits/ios_base.h: In member function `
   std::basic_ios<char, std::char_traits<char> >& std::basic_ios<char,
   std::char_traits<char> >::operator=(const std::basic_ios<char,
   std::char_traits<char> >&)':
/usr/include/c++/3.3.1/bits/ios_base.h:671: error: `std::ios_base&
   std::ios_base::operator=(const std::ios_base&)' is private
mycout.cpp:73: error: within this context
################

For a while I asked myself, is it possible that the problem
that the first program didn't work is in the .h-header files?
First I thought that made sense, but then I tried to compile
this program (using headers without ".h").

//WORKS WITH CYGWIN G++
//BUT NOT WITH CL.EXE (VC++) (compiles, but error when executed)
//################################
#include <iostream>
#include <sstream>
using namespace std;

int main ()
{

  int val;
  string mystr;
  stringstream ss (stringstream::in | stringstream::out);

  ss << "120 42 377 6 5 2000";
  cout.rdbuf(ss.rdbuf());

  for (int n=0; n<6; n++)
  {
    ss >> val;
    cerr << val*2 << '\n';
  }

  cerr << "Hello World! " << '\n';

  return 0;
}
//################################

This compiles and links without problem, and when executed all output
is sent to STDERR (also the last "Hello World" is outputted), but then
the program crashes with this error message:
The instruction at "0x00402f7f" referenced memory at "0x0000000sd".
The memory could not be "read".

The program does work fine Cygwin g++ (3.3.1)! But not with VC 6.0.
(compiled with "g++ test.cpp" and "cl /GX /TP test.cpp" (also compiled
with the entire development VC GUI, but with the same result)).

So now I don't know, is there a problem with my C++-code, or can my VC
be corrupt? Or is there some other explanation?

Once again, thanks to all of you helping me out with this!
Any help, ideas, clues or information is very much appreciated.

/Omid



Relevant Pages

  • Re: Automatically generate variables
    ... use any non-standard stuff in its own headers. ... I did *not* say the program was strictly conforming. ... See, it does compile. ... We are not discussing quality of microsoft implementation, ...
    (comp.lang.c)
  • Re: hid.dll & VC++ 6
    ... VC6 is too old. ... Many of the headers in the current WDK will not compile ... missing ')' before identifier 'HIDP_REPORT_TYPE' ...
    (microsoft.public.win32.programmer.kernel)
  • Re: resolving linker errors
    ... You can remove them from the solution, and the project would still compile. ... That means they are checked out of source control when you modify them, and the make process will monitro changes on the files and automatically recompile all dependend source files. ... If you do not add the headers, changes in these headers might not cause the moduels using the header to be recompiled. ... project likely uses lots of .h files that are not listed in the Solution Explorer - standard library headers, Windows headers, perhaps third-party libraries. ...
    (microsoft.public.vc.language)
  • Re: System headers, compiler conformance...
    ... When I compile it by invoking the compiler in conforming mode, ... Try using gcc -E to inspect the output of the pre-processor. ... See if the headers are there. ... don't use the c99 features in question. ...
    (comp.unix.programmer)
  • Re: Cygwin and g++
    ... run under Win32 without CygWin (cygwin1.dll is required, ... you get GCC both under Cygwin and MinGW. ... Linux APIs, you won't be able to compile it with MinGW, unless you do some ... The executable runned under cygwin works fine, but when I execute it ...
    (comp.os.linux.development.apps)