Yagh....ifstream

Started by
3 comments, last by NIm 17 years, 7 months ago
I'm having trouble getting this to compile.

boost::shared_ptr<model> model::loadmodel(const string filename)
{
    fstream file;
    file.open(filename);
It seems to want filename as a const char*, so how do I convert a string to an array of chars? I tried filename.c_str, but it thought that was <unknown type>
Advertisement
filename.c_str() should work fine, or I suppose you could just make a char array and copy your string elements into it a character at a time...
well, make the function parameter a const character array. and then when you call it you can just do: filename.c_str() in the call.

If you tried this, sorry.
Quote:Original post by NIm
I tried filename.c_str, but it thought that was <unknown type>

You probably forgot to #include <string>. filename.c_str() is the correct way to to it.

You shound probably pass your filename by reference rather than value (const string & filename) and open the file when you initialise it (fstream file(filename.c_str());), since this is more idiomatic C++.

Σnigma
Yagh.... I'm a dumbass. I forgot the () to make c_str a function call. I had everything you suggested

This topic is closed to new replies.

Advertisement