Jump to page: 1 2
Thread overview
Unexpected behavior with redirect cout to file
Oct 30, 2002
Richard
Oct 30, 2002
Christof Meerwald
Oct 30, 2002
bw
Oct 31, 2002
bw
Oct 31, 2002
Larry Brasfield
Oct 31, 2002
bw
Oct 31, 2002
Larry Brasfield
Oct 31, 2002
bw
Oct 31, 2002
Larry Brasfield
Oct 31, 2002
bw
Oct 31, 2002
Richard
October 30, 2002
Something a little odd here..

I'm using the 8.30 compiler.

If I do like:

ofstream log("Message.log");
streambuf* out = cout.rdbuf(log.rdbuf());

The compiler say:

sc WinsockBase.cpp -cpp -Ae -Aa -mn -C -WA -S -3 -a8 -c -gf -D_CONSOLE=1 -D_MT=1
-D_STLP_NO_NEW_IOSTREAMS=1 -oWinsockBase.obj
Error: D:\APPS\DIGITAL_MARS\AUDITOR\WinsockBase.cpp(65): 0 actual arguments
expected for ios::rdbuf, had 1
Lines Processed: 145379  Errors: 1  Warnings: 0
Build failed

Perhaps I have something misconfigured again?

If this no work, is there some other easy way to redirect cout to a file?

Richard


October 30, 2002
On Wed, 30 Oct 2002 10:44:19 +0000 (UTC), Richard wrote:
> ofstream log("Message.log");
> streambuf* out = cout.rdbuf(log.rdbuf());
> 
> The compiler say:
> 
> sc WinsockBase.cpp -cpp -Ae -Aa -mn -C -WA -S -3 -a8 -c -gf -D_CONSOLE=1 -D_MT=1
> -D_STLP_NO_NEW_IOSTREAMS=1 -oWinsockBase.obj
> Error: D:\APPS\DIGITAL_MARS\AUDITOR\WinsockBase.cpp(65): 0 actual arguments
> expected for ios::rdbuf, had 1

DM's iostream library doesn't support that function - you will have to use STLport's iostream library to make it work (but it is still more or less untested and you will probably run into unresolved external symbols at link time because DM doesn't yet support explicit template instantiation)


bye, Christof

-- 
http://cmeerw.org                                 JID: cmeerw@jabber.at mailto cmeerw at web.de

...and what have you contributed to the Net?
October 30, 2002
ofstream log("Message.log");
streambuf* out = log.rdbuf();
cout=out;
cout << "this is text for cout";

In article <apod63$11cp$1@digitaldaemon.com>, Richard says...
>
>Something a little odd here..
>
>I'm using the 8.30 compiler.
>
>If I do like:
>
>ofstream log("Message.log");
>streambuf* out = cout.rdbuf(log.rdbuf());
>
>The compiler say:
>
>sc WinsockBase.cpp -cpp -Ae -Aa -mn -C -WA -S -3 -a8 -c -gf -D_CONSOLE=1 -D_MT=1
>-D_STLP_NO_NEW_IOSTREAMS=1 -oWinsockBase.obj
>Error: D:\APPS\DIGITAL_MARS\AUDITOR\WinsockBase.cpp(65): 0 actual arguments
>expected for ios::rdbuf, had 1
>Lines Processed: 145379  Errors: 1  Warnings: 0
>Build failed
>
>Perhaps I have something misconfigured again?
>
>If this no work, is there some other easy way to redirect cout to a file?
>
>Richard
>
>


October 31, 2002
hmmm, actually now that i took a better look, seems you can assign an ofstream object directly to cout...

ofstream log("Message.log");
cout=log;
cout << "this text goes to message.log\n";


In article <aposs0$nun$1@digitaldaemon.com>, bw says...
>
>ofstream log("Message.log");
>streambuf* out = log.rdbuf();
>cout=out;
>cout << "this is text for cout";



October 31, 2002
In article <appsp4$2c0v$1@digitaldaemon.com>, bw (bw_member@pathlink.com) says...
> hmmm, actually now that i took a better look, seems you can assign an ofstream object directly to cout...
> 
> ofstream log("Message.log");
> cout=log;
> cout << "this text goes to message.log\n";

That is really bad advice.  iostream objects are
not supposed to be assignable.  The fact that you
got it to compile with some old C++ library does
not make such assignment a good idea.

(To the OP)
If you want to redirect cout itself, look at the
rdbuf() overloads.  One can be used to extract a
streambuf from an iostream and the other can be
used to make an iostream used a given streambuf.
By making cout use a streambuf of your choice,
you can effectively redirect output sent to the
cout object in a way that standard C++ supports.

-- 
-Larry Brasfield
(address munged, s/sn/h/ to reply)
October 31, 2002
lmao, wasn't really meant to be advice.  and cout is assignable from way back, all of my out-of-date books say so... that's what i thought class ostream_with_assign was for?

how bout some code hot dawg?
thanks,
bw

In article <MPG.182a371359eaa71a989698@news.digitalmars.com>, Larry Brasfield says...
>
>In article <appsp4$2c0v$1@digitaldaemon.com>, bw (bw_member@pathlink.com) says...
>> hmmm, actually now that i took a better look, seems you can assign an ofstream object directly to cout...
>> 
>> ofstream log("Message.log");
>> cout=log;
>> cout << "this text goes to message.log\n";
>
>That is really bad advice.  iostream objects are
>not supposed to be assignable.  The fact that you
>got it to compile with some old C++ library does
>not make such assignment a good idea.
>
>(To the OP)
>If you want to redirect cout itself, look at the
>rdbuf() overloads.  One can be used to extract a
>streambuf from an iostream and the other can be
>used to make an iostream used a given streambuf.
>By making cout use a streambuf of your choice,
>you can effectively redirect output sent to the
>cout object in a way that standard C++ supports.
>
>-- 
>-Larry Brasfield
>(address munged, s/sn/h/ to reply)


October 31, 2002
In article <MPG.182a371359eaa71a989698@news.digitalmars.com>, Larry Brasfield says...

Title: Re: Unexpected behavior with redirect cout to file Author: Larry Brasfield <larry_brasfield@snotmail.com> Date: Wed, 30 Oct 2002 18:36:39 -0800

snotmail.com?  dang, need a hanky?


October 31, 2002
In article <apq5s6$2ku6$1@digitaldaemon.com>, bw (bw_member@pathlink.com) says...
> lmao, wasn't really meant to be advice.

Since it appears to be a possible solution
to the OP's problem, and since you did not
indicate otherwise, your assertion "you
can assign to an ofstream" has a lot of
similarity to advice.  Since it was not
so meant, let me correct myself:

Assigning to cout is a bad idea and will
compromise portability to conforming C++
implementations.

> and cout is assignable from way back,
> all of my out-of-date books say so... that's what i thought class
> ostream_with_assign was for?

That class is not part of standard C++.
And as I stated before, the problem you
might want to solve by assigning stream
objects is provided for via rdbuf().

The object named cout (or std::cout) is
guaranteed only to be an instance of
std::ostream, not of ostream_with_assign.

I do not deny that some early C++
implementors thought stream assignment
was worthwhile, but the C++ committee
thought better of it and provided a
more tractable method of redirection.
I think some folks shuddered at the
thought of defining the semantics of
parallel stream "copies".

> how bout some code hot dawg?

#include <iostream>
using std::cout;
#include <fstream>

int main(int na, char *ap[])
{
  if (na > 1) {
    std::ofstream fout(ap[1]);
    if (fout) {
      // Extract the buffer from just opened file.
      std::streambuf * fobuf = fout.rdbuf();
      // Save the original stdout buffer.
      std::streambuf * sobuf = cout.rdbuf();
      // Redirect cout output to the opened file.
      cout.rdbuf(fobuf);
      // Demonstrate redirection.
      cout << "This verbage was put onto cout.\n";
      // Restore cout to original state.
      cout.rdbuf(sobuf);
    }
    else
      std::cerr << "Cannot write " << ap[1] << std::endl;
  }
}

The above is standard C++ and may need
a few adjustments to get past DMC++.
The STLPort library should like it.

> thanks,

You're welcome.

> bw

> In article <MPG.182a371359eaa71a989698@news.digitalmars.com>, Larry Brasfield says...
> >
> >In article <appsp4$2c0v$1@digitaldaemon.com>, bw (bw_member@pathlink.com) says...
> >> hmmm, actually now that i took a better look, seems you can assign an ofstream object directly to cout...
> >> 
> >> ofstream log("Message.log");
> >> cout=log;
> >> cout << "this text goes to message.log\n";
> >
> >That is really bad advice.  iostream objects are
> >not supposed to be assignable.  The fact that you
> >got it to compile with some old C++ library does
> >not make such assignment a good idea.
> >
> >(To the OP)
> >If you want to redirect cout itself, look at the
> >rdbuf() overloads.  One can be used to extract a
> >streambuf from an iostream and the other can be
> >used to make an iostream used a given streambuf.
> >By making cout use a streambuf of your choice,
> >you can effectively redirect output sent to the
> >cout object in a way that standard C++ supports.

-- 
-Larry Brasfield
(address munged, s/sn/h/ to reply)
October 31, 2002
In article <apq60q$2kv5$1@digitaldaemon.com>, bw (bw_member@pathlink.com) says...
> In article <MPG.182a371359eaa71a989698@news.digitalmars.com>, Larry Brasfield says...
> 
> Title: Re: Unexpected behavior with redirect cout to file Author: Larry Brasfield <larry_brasfield@snotmail.com> Date: Wed, 30 Oct 2002 18:36:39 -0800
> 
> snotmail.com?  dang, need a hanky?

If you substitute the 'sn' with 'h', as
indicated in my sig, the snot vanishes
without need of a hanky.

-- 
-Larry Brasfield
(address munged, s/sn/h/ to reply)
October 31, 2002
thanks for the code larry, i'm working on it, honest.. about 5 years behind i guess but i'm all for the "modern" thing.  in the meantime i kinda have to go with what works and risk being wrong now and then... ah well.

you know i think i'm beginning to see why conformance might be so controversial. seems like it could be tough (and costly) to keep a product current.  possibly stranded, if you implement only some features and don't finish up before the standard changes.

In article <MPG.182a797d556f0a4698969d@news.digitalmars.com>, Larry Brasfield says...
>> snotmail.com?  dang, need a hanky?
>
>If you substitute the 'sn' with 'h', as
>indicated in my sig, the snot vanishes
>without need of a hanky.



« First   ‹ Prev
1 2