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

Tags: •  •  • 

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…


Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.

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


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.


Post new comment

The content of this field is kept private and will not be shown publicly.
  • You may post code using <code>...</code> (generic) or <?php ... ?> (highlighted PHP) tags.
  • You can use Markdown syntax to format and style the text.
  • Images can be added to this post.
  • You may use [inline:xx] tags to display uploaded files or images inline.
More information about formatting options