std::string and fstream

Started by
5 comments, last by dnsauve 19 years ago
Just curious about why I can't pass a std::string to an fstream constructor. Am I the only person that thinks it's odd that you have to pass a char array?
Advertisement
use the .c_str() function of std::string for a c-style char array representation of the string

EDIT: I also found it rather odd.
Thanks, this actually what I've been doing... Doesn't make for very elegant looking code though. Yuck.

Is this just Microsoft's implementation of STL or is that just the way it is?
I would say it's the standard. And consider it rather sane.
You should always strive to decouple things as much as possible and this is an excellent example. Having std::fstream dependant on std::string would be nutty what if clients don't need want desire or even afford (memory can be expensive sometimes) to link to std::string why would they need to when all the ctor really wants is a string representing a filename?
HardDrop - hard link shell extension."Tread softly because you tread on my dreams" - Yeats
Fair enough.

But, why couldn't the fstream ctor be overloaded to take a std::string as well as the standard char array?
Quote:Original post by dnsauve
Fair enough.

But, why couldn't the fstream ctor be overloaded to take a std::string as well as the standard char array?


Since then it would be dependant on std::string anyhow because it's visible in the interface.

But if it bothers you why not simply create a convinence function to handle it that's how you usually glue togheter libraries :)
HardDrop - hard link shell extension."Tread softly because you tread on my dreams" - Yeats
Good idea. Thanks.

This topic is closed to new replies.

Advertisement