Today's g++/C++ funny: ofstream's open constructor

So, I'm working on a project, and have some code like this:

string blah="somefile"; ofstream fp;

fp.open(blah);

And it doesn't work. Apparently, what you need to do when using ofstream's open method:

fp.open(blah.c_str());

Yes, it looks like what it is. Apparently g++'s iostreams library (gcc 3.2) does not support things that have been part of the C++ standard for years--like C++ STL strings... You have to convert back to a C string.

Last I checked this was 2005, not 1999. You'd have thought someone would have overloaded this by now... I've only checked this in g++; will check with Intel's C++ compiler and Microsoft Visual Studio later, though I'm not sure whether to expect better. What didn't help me to find this out, of course, is g++'s totally wonderful (sarcasm) compiler error messages...

Trackback URL for this post:

http://samat.org/trackback/41

Comments

g++’s iostreams library

g++'s iostreams library follows the standard, which says ofstream does not have such an open() overload. Blame C++, not g++.

The C++ standard library was gathered together from several adhoc libs, like HP's STL and somedude's iostreams, and practically no integrating was done. This will be looked at in the next C++ standardization release, C++0x.

THANK YOU!

I'm an experienced developer...but REAL new to using C++ (As in about 3 days of self-immersion). I'm trying to write a simple utility before taking a more structured approach to learning the language...and was having this EXACT problem! Thank You, this is the roadblock I was having this morning...I can now open files I've READ from the direcotry. I can finally continue with my simple utility and feel I've accomplished SOMETHING useful!

Oh, as for compiler, I'm using Bloodshed's Dev-C on Windows XP.

Mike Dziedzic