Discussion:
boost::shared_ptr<std::ofstream> don't write to file
Meir Yanovich
2008-05-21 12:38:43 UTC
Permalink
Hello all
im using boost::shared_ptr to init std::ofstream in it
im declaring it as static :
the .h file :

typedef boost::shared_ptr<std::ofstream> OFStreamPtrType;

class Foo {

static OFStreamPtrType iStream;



}

then in the cpp file :

OFStreamPtrType Utilities::iStream(new ofstream("mylog.txt",ios::app));

Foo::printLog(string s){

if (Foo::iStream->is_open())
{
Foo::iStream << c <<".\n";
}

}

no its opening the file mylog.txt but it is not writing any thing to it
why ?
Igor R.
2008-05-21 12:42:57 UTC
Permalink
no its opening the file mylog.txt but it is not writing any thing to it> why ?
probably, you test it before it flushes?
try to flush() your stream, or disable buffering (oStream->rdbuf()->pubsetbuf(0, 0);)
_________________________________________________________________
Explore the seven wonders of the world
http://search.msn.com/results.aspx?q=7+wonders+world&mkt=en-US&form=QBRE
Steven Watanabe
2008-05-21 13:34:47 UTC
Permalink
AMDG
Post by Meir Yanovich
typedef boost::shared_ptr<std::ofstream> OFStreamPtrType;
class Foo {
static OFStreamPtrType iStream;
}
OFStreamPtrType Utilities::iStream(new ofstream("mylog.txt",ios::app));
Foo::printLog(string s){
if (Foo::iStream->is_open())
{
Foo::iStream << c <<".\n";
}
}
no its opening the file mylog.txt but it is not writing any thing to it
why ?
It might help to dereference the pointer.

*Foo::iStream << c <<".\n";


In Christ,
Steven Watanabe
Igor R.
2008-05-21 14:13:59 UTC
Permalink
It might help to dereference the pointer.> > *Foo::iStream << c <<".\n";> >
I thought he just misstyped it in this example. Is there the operator << (shared_ptr<...>, const string&) ?!
_________________________________________________________________
Invite your mail contacts to join your friends list with Windows Live Spaces. It's easy!
http://spaces.live.com/spacesapi.aspx?wx_action=create&wx_url=/friends.aspx&mkt=en-us
Meir Yanovich
2008-05-21 14:28:59 UTC
Permalink
when im doing *Foo::iStream << c <<".\n";
im getting
error C2100: illegal indirection
Post by Igor R.
Post by Steven Watanabe
It might help to dereference the pointer.
*Foo::iStream << c <<".\n";
I thought he just misstyped it in this example. Is there the operator <<
(shared_ptr<...>, const string&) ?!
________________________________
Invite your mail contacts to join your friends list with Windows Live
Spaces. It's easy! Try it!
_______________________________________________
Boost-users mailing list
http://lists.boost.org/mailman/listinfo.cgi/boost-users
Igor R.
2008-05-21 14:46:07 UTC
Permalink
You probably didn't put here the *exact* code that you test.Anyway: apply << operator to the stream, not to the pointer or shared_ptr; ensure you flushed the stream.> > when im doing *Foo::iStream << c <<".\n";> im getting> error C2100: illegal indirection>
_________________________________________________________________
News, entertainment and everything you care about at Live.com. Get it now!
http://www.live.com/getstarted.aspx
Meir Yanovich
2008-05-22 04:52:53 UTC
Permalink
Thanks its working now
can you please explain me in short what is the benefits of boost::shared_ptr?
thanks
Post by Igor R.
You probably didn't put here the *exact* code that you test.
Anyway: apply << operator to the stream, not to the pointer or shared_ptr;
ensure you flushed the stream.
Post by Meir Yanovich
when im doing *Foo::iStream << c <<".\n";
im getting
error C2100: illegal indirection
________________________________
Get news, entertainment and everything you care about at Live.com. Check it
out!
_______________________________________________
Boost-users mailing list
http://lists.boost.org/mailman/listinfo.cgi/boost-users
Kevin Martin
2008-05-21 14:45:31 UTC
Permalink
Post by Meir Yanovich
when im doing *Foo::iStream << c <<".\n";
im getting
error C2100: illegal indirection
I think this code is roughly equivalent to what you're trying to do,
and it compiles with both g++ and icc. Test if you can compile this,
if so your problem is somewhere else.

#include <boost/shared_ptr.hpp>
#include <sstream>
#include <iostream>
#include <ostream>

class X
{
public:
static boost::shared_ptr<std::ostringstream> x;
static void init_stream();
};

boost::shared_ptr<std::ostringstream> X::x;

void X::init_stream() {
boost::shared_ptr<std::ostringstream> temp(new std::ostringstream);
x.swap(temp);
}

int main() {
X::init_stream();

*X::x << "Hello" << std::flush;

std::cout << X::x->str() << std::endl;
return 0;
}
Loading...