473,803 Members | 3,095 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

? Why c_str() ?

Hi.
Compilation of this program is OK :
---------------------
#include <string>
#include <fstream>
using namespace std ;
int main() {
ofstream myfile;
string FileName="Sampl e";
myfile.open((Fi leName).c_str() );
myfile.close();
}
--------------------
but this one fails :
#include <string>
#include <fstream>
using namespace std ;
int main() {
ofstream myfile;
string FileName="Sampl e";
myfile.open(Fil eName);
myfile.close();
}
and the error message is:
no matching function for call to `std::basic_ofs tream<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(Fi leName);" ?
What does the c_str() function ?

TIA
Erkson

Jul 19 '05 #1
4 26653
"Ericcson" <Er******@wanad oo.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="Sampl e";
myfile.open((Fi leName).c_str() );
myfile.close();
}
--------------------
but this one fails :
#include <string>
#include <fstream>
using namespace std ;
int main() {
ofstream myfile;
string FileName="Sampl e";
myfile.open(Fil eName);
myfile.close();
}
and the error message is:
no matching function for call to `std::basic_ofs tream<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(Fi leName);" ?

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.merke rk(at)dse.nl

Jul 19 '05 #2
On Mon, 28 Jul 2003 12:12:41 +0200, Ericcson <Er******@wanad oo.fr> wrote:
Hi.
Compilation of this program is OK :
---------------------
#include <string>
#include <fstream>
using namespace std ;
int main() {
ofstream myfile;
string FileName="Sampl e";
myfile.open((Fi leName).c_str() );
myfile.close();
}
--------------------
but this one fails :
#include <string>
#include <fstream>
using namespace std ;
int main() {
ofstream myfile;
string FileName="Sampl e";
myfile.open(Fil eName);
myfile.close();
}
and the error message is:
no matching function for call to `std::basic_ofs tream<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(Fi leName);" ?
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******@wanad oo.fr>
wrote:
Hi.
Compilation of this program is OK :
---------------------
#include <string>
#include <fstream>
using namespace std ;
int main() {
ofstream myfile;
string FileName="Sampl e";
myfile.open((Fi leName).c_str() );
myfile.close();
}
--------------------
but this one fails :
#include <string>
#include <fstream>
using namespace std ;
int main() {
ofstream myfile;
string FileName="Sampl e";
myfile.open(Fil eName);
myfile.close();
}
and the error message is:
no matching function for call to `std::basic_ofs tream<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(Fi leName);" ?
What does the c_str() function ?
you could...

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

myfile.open(fil e);
myfile.close();

Charles

TIA
Erkson


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

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
11614
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 so they can return a pointer to their internal buffer, or are they equally likely to create a new buffer on demand? I know the standard doesn't require any particular implementation, which is why I'm curious if there is a consensus among...
2
3095
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 instances of std::string in that implementation. I did not find anything that
2
3669
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
1933
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 string.
4
6910
by: Alex Vinokur | last post by:
I have got two functions: void foo1(char* str) { // Stuff } void foo2(int size) { char* str;
2
6458
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
5286
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 strings. This works fine but recently after close to 1000 successful runs my
3
2618
by: C++Liliput | last post by:
I have a class of the type class A { private: std::string data; ....... public: const char* toString(); };
8
6321
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
10550
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10317
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10295
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9125
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6844
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5501
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4275
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3799
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2972
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.