? Why c_str() ?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Ericcson

    ? 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



  • Peter van Merkerk

    #2
    Re: ? Why c_str() ?

    "Ericcson" <Ericcson@wanad oo.fr> wrote in message
    news:bg2svh$ei6 $1@news-reader5.wanadoo .fr...[color=blue]
    > 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.[/color]

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

    No 'const char*' is not the same as a 'std::string' class.
    [color=blue]
    > Is there a way to avoid c(str() it and only write[/color]
    "myfile.open(Fi leName);" ?

    You would you want to?
    [color=blue]
    > What does the c_str() function ?[/color]

    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



    Comment

    • Sam Holden

      #3
      Re: ? Why c_str() ?

      On Mon, 28 Jul 2003 12:12:41 +0200, Ericcson <Ericcson@wanad oo.fr> wrote:[color=blue]
      > 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.[/color]

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

      No. It's a std::string.
      [color=blue]
      >
      > Is there a way to avoid c(str() it and only write "myfile.open(Fi leName);" ?[/color]

      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.
      [color=blue]
      > What does the c_str() function ?[/color]

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

      --
      Sam Holden

      Comment

      • Charles

        #4
        Re: ? Why c_str() ?

        On Mon, 28 Jul 2003 12:12:41 +0200, "Ericcson" <Ericcson@wanad oo.fr>
        wrote:
        [color=blue]
        >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.[/color]
        The member function returns a pointer to a nonmodifiable C string
        constructed by adding a terminating null element to the controlled
        sequence.
        [color=blue]
        >
        >Isn't my string the "const char * filename" that requires the open function
        >void open ( const char * filename, openmode mode = out | trunc ) ?[/color]
        no, your string is a <string> class object, not a pointer to a
        null-terminated character array
        [color=blue]
        >
        >Is there a way to avoid c(str() it and only write "myfile.open(Fi leName);" ?
        >What does the c_str() function ?[/color]

        you could...

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

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

        Charles
        [color=blue]
        >
        >TIA
        >Erkson
        >
        >[/color]

        Comment

        • Kevin Goodsell

          #5
          Re: ? Why c_str() ?

          Dabroz wrote:[color=blue]
          > Uzytkownik <Charles> napisal w wiadomosci
          > news:12v9iv8rfe 3c3olg5g5gqcg2r h326sdq5f@4ax.c om...[color=green]
          >>
          >>you could...
          >>
          >>char filename[] = "Sample";
          >>char* file = filename;[/color]
          >
          >
          > Why two pointers should show same memory adress?
          >[/color]

          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.

          Comment

          Working...