? Why c_str() ?

E

Ericcson

Hi.
Compilation of this program is OK :
---------------------
#include <string>
#include <fstream>
using namespace std ;
int main() {
ofstream myfile;
string FileName="Sample";
myfile.open((FileName).c_str());
myfile.close();
}
--------------------
but this one fails :
#include <string>
#include <fstream>
using namespace std ;
int main() {
ofstream myfile;
string FileName="Sample";
myfile.open(FileName);
myfile.close();
}
and the error message is:
no matching function for call to `std::basic_ofstream<char
------------------------
I would like to understand what is c_str() function for.

Isn't my string the "const char * filename" that requires the open function
void open ( const char * filename, openmode mode = out | trunc ) ?

Is there a way to avoid c(str() it and only write "myfile.open(FileName);" ?
What does the c_str() function ?

TIA
Erkson
 
P

Peter van Merkerk

Ericcson said:
Hi.
Compilation of this program is OK :
---------------------
#include <string>
#include <fstream>
using namespace std ;
int main() {
ofstream myfile;
string FileName="Sample";
myfile.open((FileName).c_str());
myfile.close();
}
--------------------
but this one fails :
#include <string>
#include <fstream>
using namespace std ;
int main() {
ofstream myfile;
string FileName="Sample";
myfile.open(FileName);
myfile.close();
}
and the error message is:
no matching function for call to `std::basic_ofstream<char

It is needed to get a const char* representation of the text stored
inside a std::string class.
Isn't my string the "const char * filename" that requires the open function
void open ( const char * filename, openmode mode = out | trunc ) ?

No 'const char*' is not the same as a 'std::string' class.
Is there a way to avoid c(str() it and only write
"myfile.open(FileName);" ?

You would you want to?
What does the c_str() function ?

See above, it is mainly used for interfacing. Don't ask me why
ofstream::eek:pen() takes a const char* instead of a std::string, I have
really no clue whatsoever. Unfortunately the std::string::c_str()
function is still needed in quite a few cases. Often GUI libraries have
their own string class with the std::string::c_str() function often
being the only way to convert between the two.
 
S

Sam Holden

Hi.
Compilation of this program is OK :
---------------------
#include <string>
#include <fstream>
using namespace std ;
int main() {
ofstream myfile;
string FileName="Sample";
myfile.open((FileName).c_str());
myfile.close();
}
--------------------
but this one fails :
#include <string>
#include <fstream>
using namespace std ;
int main() {
ofstream myfile;
string FileName="Sample";
myfile.open(FileName);
myfile.close();
}
and the error message is:
no matching function for call to `std::basic_ofstream<char

It returns a C style string representation of the data in the std::string
object - ie. a const char*.
Isn't my string the "const char * filename" that requires the open function > void open ( const char * filename, openmode mode = out | trunc ) ?

No. It's a std::string.
Is there a way to avoid c(str() it and only write "myfile.open(FileName);" ?

const char *FileName = "Sample";

If you want to use a std::string then no. This is silly in my opinion,
but the streams predate the strings, I guess anyway.
What does the c_str() function ?

It returns a const char* representation of the std::string object, but
you already asked that, and I already answered it :)
 
C

Charles

Hi.
Compilation of this program is OK :
---------------------
#include <string>
#include <fstream>
using namespace std ;
int main() {
ofstream myfile;
string FileName="Sample";
myfile.open((FileName).c_str());
myfile.close();
}
--------------------
but this one fails :
#include <string>
#include <fstream>
using namespace std ;
int main() {
ofstream myfile;
string FileName="Sample";
myfile.open(FileName);
myfile.close();
}
and the error message is:
no matching function for call to `std::basic_ofstream<char
The member function returns a pointer to a nonmodifiable C string
constructed by adding a terminating null element to the controlled
sequence.
Isn't my string the "const char * filename" that requires the open function
void open ( const char * filename, openmode mode = out | trunc ) ?
Is there a way to avoid c(str() it and only write "myfile.open(FileName);" ?
What does the c_str() function ?

you could...

char filename[] = "Sample";
char* file = filename;

myfile.open(file);
myfile.close();

Charles
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top