473,320 Members | 1,699 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

? Why c_str() ?

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

Jul 19 '05 #1
4 26624
"Ericcson" <Er******@wanadoo.fr> wrote in message
news:bg**********@news-reader5.wanadoo.fr...
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.
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::open() 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.

--
Peter van Merkerk
peter.van.merkerk(at)dse.nl

Jul 19 '05 #2
On Mon, 28 Jul 2003 12:12:41 +0200, Ericcson <Er******@wanadoo.fr> wrote:
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.
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 :)

--
Sam Holden

Jul 19 '05 #3
On Mon, 28 Jul 2003 12:12:41 +0200, "Ericcson" <Er******@wanadoo.fr>
wrote:
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. 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 ) ? no, your string is a <string> class object, not a pointer to a
null-terminated character array

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

TIA
Erkson


Jul 19 '05 #4
Dabroz wrote:
Uzytkownik <Charles> napisal w wiadomosci
news:12********************************@4ax.com...

you could...

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

Why two pointers should show same memory adress?


filename is not a pointer. It is an array.

-Kevin
--
My email address is valid, but changes periodically.
To contact me please use the address from a recent posting.

Jul 19 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

15
by: Derek | last post by:
I'm curious about the performance of string::c_str, so I'm wondering how it's commonly implemented. Do most std::string implementations just keep an extra char allocated for the NULL termination...
2
by: Vyacheslav Kononenko | last post by:
All, If I am not mistaken I had some problems with code like this: std::string foo, bar; .... somefunc( foo.c_str(), bar.c_str() ); Problem was that c_str() used buffer shared btw...
2
by: diadia | last post by:
string s = "hello"; const char *p = s.begin(); cout << p << endl; // print hello s = ""; char *p2= s.begin(); cout << p2 << endl; // print hello why?????
5
by: Fred Paris | last post by:
Hi I'm writing a class to act as a wrapper around a C library. This C library exposes functions like: SetSomeInfo( char *pTheInfo ); In my wrapper class, the info in question is in a STL...
4
by: Alex Vinokur | last post by:
I have got two functions: void foo1(char* str) { // Stuff } void foo2(int size) { char* str;
2
by: ma740988 | last post by:
Consider: bool transmit ( const char* pch, size_t len ) { int const val = strcmp( pch, "who_am_i" ); if ( val == 0 ) return ( true ); return ( false ); }
12
by: shyam | last post by:
Hi I have a program within which i have the following statement. sprintf(somevar,"%s/%s.%s",PREFIX.c_str( ),MIDDLE.c_str( ),SUFFIX.c_str( ) ); Here PREFIX, MIDDLE and SUFFIX are all const...
3
by: C++Liliput | last post by:
I have a class of the type class A { private: std::string data; ....... public: const char* toString(); };
8
by: puzzlecracker | last post by:
Any ideas what may cause that for the string class to core dump? #0 0x0018de92 in std::string::c_str () from /usr/lib/libstdc++.so.5
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.