Why can't a C++ string be used as the path name to open a file but a C-string can?

Discussion in 'C++' started by solartimba, Oct 17, 2003.

  1. solartimba

    solartimba Guest

    Why can I open a file using a C-string but not a C++ string?

    //C-strings works
    char path[15];
    strcpy(path,"c:\test.txt");
    ifstream infile(path);

    //C++ string class does not
    string path("c:\test.txt");
    ifstream infile(path);

    Why? Is there something I can do to use a string (maybe a recast??)?
     
    solartimba, Oct 17, 2003
    #1
    1. Advertising

  2. solartimba

    cpp_weenie Guest

    "solartimba" <> wrote in message
    news:...
    > Why can I open a file using a C-string but not a C++ string?
    >
    > //C-strings works
    > char path[15];
    > strcpy(path,"c:\test.txt");
    > ifstream infile(path);
    >
    > //C++ string class does not
    > string path("c:\test.txt");
    > ifstream infile(path);
    >
    > Why? Is there something I can do to use a string (maybe a recast??)?


    Do this:

    ifstream infile(path.c_str());
     
    cpp_weenie, Oct 17, 2003
    #2
    1. Advertising

  3. solartimba

    Mike Wahler Guest

    "solartimba" <> wrote in message
    news:...
    > Why can I open a file using a C-string but not a C++ string?
    >
    > //C-strings works
    > char path[15];
    > strcpy(path,"c:\test.txt");


    strcpy(path,"c:\\test.txt");

    > ifstream infile(path);
    >
    > //C++ string class does not
    > string path("c:\test.txt");


    string path("c:\\test.txt");

    (look up 'escape character').

    > ifstream infile(path);
    >
    > Why? Is there something I can do to use a string (maybe a recast??)?


    ifstream infile(path.c_str());

    Note that 'c_str()' returns a pointer to *const* chars,
    so don't try to change those characters.

    -Mike
     
    Mike Wahler, Oct 17, 2003
    #3
  4. solartimba

    Cy Edmunds Guest

    "solartimba" <> wrote in message
    news:...
    > Why can I open a file using a C-string but not a C++ string?
    >
    > //C-strings works
    > char path[15];
    > strcpy(path,"c:\test.txt");
    > ifstream infile(path);
    >
    > //C++ string class does not
    > string path("c:\test.txt");
    > ifstream infile(path);
    >
    > Why? Is there something I can do to use a string (maybe a recast??)?


    Of course you can use c_str() as the others said but it seems to me you
    should be able to use std::string for just about any string argument in the
    standard library. Let's hope the next revision of the standard fixes this.

    --
    Cy
    http://home.rochester.rr.com/cyhome/
     
    Cy Edmunds, Oct 17, 2003
    #4
  5. solartimba

    lilburne Guest

    Re: Why can't a C++ string be used as the path name to open a filebut a C-string can?

    Cy Edmunds wrote:

    > "solartimba" <> wrote in message
    > news:...
    >
    >>Why can I open a file using a C-string but not a C++ string?
    >>
    >>//C-strings works
    >>char path[15];
    >>strcpy(path,"c:\test.txt");
    >>ifstream infile(path);
    >>
    >>//C++ string class does not
    >>string path("c:\test.txt");
    >>ifstream infile(path);
    >>
    >>Why? Is there something I can do to use a string (maybe a recast??)?

    >
    >
    > Of course you can use c_str() as the others said but it seems to me you
    > should be able to use std::string for just about any string argument in the
    > standard library. Let's hope the next revision of the standard fixes this.
    >


    String has been about for quite a few years. If it needed a
    conversion to a char type it would have been added by now.
    But it isn't required, and previous string classes that had
    char conversion were found to be a nuisence. Automatic type
    conversions using operator() will cause you nothing but
    grief unless you liberally sprinkle 'explicit' through your
    code.
     
    lilburne, Oct 17, 2003
    #5
  6. solartimba

    Mike Wahler Guest

    "Cy Edmunds" <> wrote in message
    news:jf_jb.29877$...
    > Of course you can use c_str() as the others said but it seems to me you
    > should be able to use std::string for just about any string argument in

    the
    > standard library. Let's hope the next revision of the standard fixes this.


    I for one, hope not. :)


    -Mike
     
    Mike Wahler, Oct 18, 2003
    #6
  7. In article <jf_jb.29877$>,
    Cy Edmunds <> wrote:
    >Of course you can use c_str() as the others said but it seems to me you
    >should be able to use std::string for just about any string argument in the
    >standard library. Let's hope the next revision of the standard fixes this.


    If you mean that the functions in the standard library which take C strings
    should also take std::string then I agree with that.

    If you mean that the std::string should automatically convert to a C string
    then I don't think that is a good idea.

    John
     
    John Villalovos, Oct 18, 2003
    #7
  8. Mike Wahler wrote in
    news:FE_jb.652$:

    > "Cy Edmunds" <> wrote in message
    > news:jf_jb.29877$...
    >> Of course you can use c_str() as the others said but it seems to me
    >> you should be able to use std::string for just about any string
    >> argument in

    > the
    >> standard library. Let's hope the next revision of the standard fixes
    >> this.

    >
    > I for one, hope not. :)
    >


    Do you mean no std::string::eek:perator char const *(), or
    so you mean no std::ifstream( std::string const & ).

    I'd agree with the former, don't much care about the latter.
    Though maybe boost::filesystem::path (std::tr2?) would be
    better.

    Rob.
    --
    http://www.victim-prime.dsl.pipex.com/
     
    Rob Williscroft, Oct 18, 2003
    #8
  9. solartimba

    Mike Wahler Guest

    "Rob Williscroft" <> wrote in message
    news:Xns941810BED101FukcoREMOVEfreenetrtw@195.129.110.130...
    > Mike Wahler wrote in
    > news:FE_jb.652$:
    >
    > > "Cy Edmunds" <> wrote in message
    > > news:jf_jb.29877$...
    > >> Of course you can use c_str() as the others said but it seems to me
    > >> you should be able to use std::string for just about any string
    > >> argument in

    > > the
    > >> standard library. Let's hope the next revision of the standard fixes
    > >> this.

    > >
    > > I for one, hope not. :)
    > >

    >
    > Do you mean no std::string::eek:perator char const *(),



    Yes.

    >or
    > so you mean no std::ifstream( std::string const & ).


    Not that, I think that would indeed be useful.

    >
    > I'd agree with the former, don't much care about the latter.


    And I agree with you. :)

    > Though maybe boost::filesystem::path (std::tr2?) would be
    > better.


    I'd have to think about that. :)

    Thanks for your input.

    -Mike
     
    Mike Wahler, Oct 18, 2003
    #9
  10. > Of course you can use c_str() as the others said but it seems to me you
    > should be able to use std::string for just about any string argument in

    the
    > standard library. Let's hope the next revision of the standard fixes this.


    The reason it wasn't done was that a number of committee members wanted
    to deal with regular strings and wide strings at the same time. To do
    otherwise
    would have strengthened the bias against countries, such as Japan, that use
    wide strings as their ordinary way of expressing text.
     
    Andrew Koenig, Oct 18, 2003
    #10
  11. solartimba

    Cy Edmunds Guest

    "Mike Wahler" <> wrote in message
    news:yq0kb.816$...
    >
    > "Rob Williscroft" <> wrote in message
    > news:Xns941810BED101FukcoREMOVEfreenetrtw@195.129.110.130...
    > > Mike Wahler wrote in
    > > news:FE_jb.652$:
    > >
    > > > "Cy Edmunds" <> wrote in message
    > > > news:jf_jb.29877$...
    > > >> Of course you can use c_str() as the others said but it seems to me
    > > >> you should be able to use std::string for just about any string
    > > >> argument in
    > > > the
    > > >> standard library. Let's hope the next revision of the standard fixes
    > > >> this.
    > > >
    > > > I for one, hope not. :)
    > > >

    > >
    > > Do you mean no std::string::eek:perator char const *(),

    >
    >
    > Yes.
    >
    > >or
    > > so you mean no std::ifstream( std::string const & ).

    >
    > Not that, I think that would indeed be useful.
    >
    > >
    > > I'd agree with the former, don't much care about the latter.

    >
    > And I agree with you. :)
    >
    > > Though maybe boost::filesystem::path (std::tr2?) would be
    > > better.

    >
    > I'd have to think about that. :)
    >
    > Thanks for your input.
    >
    > -Mike
    >
    >


    I just meant a parallel function or constructor which takes a std::string
    argument, not a conversion operator which I think would be a poor idea
    indeed.

    --
    Cy
    http://home.rochester.rr.com/cyhome/
     
    Cy Edmunds, Oct 18, 2003
    #11
  12. solartimba

    Cy Edmunds Guest

    "Andrew Koenig" <> wrote in message
    news:38ekb.183549$...
    > > Of course you can use c_str() as the others said but it seems to me you
    > > should be able to use std::string for just about any string argument in

    > the
    > > standard library. Let's hope the next revision of the standard fixes

    this.
    >
    > The reason it wasn't done was that a number of committee members wanted
    > to deal with regular strings and wide strings at the same time. To do
    > otherwise
    > would have strengthened the bias against countries, such as Japan, that

    use
    > wide strings as their ordinary way of expressing text.
    >
    >


    I don't see how the current "solution" addresses this need.

    --
    Cy
    http://home.rochester.rr.com/cyhome/
     
    Cy Edmunds, Oct 18, 2003
    #12
  13. solartimba

    Ron Natalie Guest

    "Cy Edmunds" <> wrote in message news:w%gkb.915$%...

    > > The reason it wasn't done was that a number of committee members wanted
    > > to deal with regular strings and wide strings at the same time. To do
    > > otherwise
    > > would have strengthened the bias against countries, such as Japan, that

    > use
    > > wide strings as their ordinary way of expressing text.
    > >
    > >

    >
    > I don't see how the current "solution" addresses this need.


    Since none of the interfaces take wide ANYTHING (wchar_t or wstrings), I agree.
    Further the standards commitee has been overly obstinate about fixing the wide
    char deficiencies in these interfaces.

    Changing these interfaces to just take either a string or a const string& would be
    largely transparent to existing code and accomplish the goal (as there is a converting
    constructor to string).
     
    Ron Natalie, Oct 18, 2003
    #13
  14. Try using
    string path("c:\test.txt");
    ifstream infile(path.c_str());

    "solartimba" <> wrote in message
    news:...
    > Why can I open a file using a C-string but not a C++ string?
    >
    > //C-strings works
    > char path[15];
    > strcpy(path,"c:\test.txt");
    > ifstream infile(path);
    >
    > //C++ string class does not
    > string path("c:\test.txt");
    > ifstream infile(path);
    >
    > Why? Is there something I can do to use a string (maybe a recast??)?
     
    Gregory Smith, Oct 20, 2003
    #14
  15. Re: Why can't a C++ string be used as the path name to open a file but aC-string can?

    Gregory Smith wrote:
    >
    > Try using
    > string path("c:\test.txt");
    > ifstream infile(path.c_str());
    >


    Note:

    string path( "c:\\test.txt" );

    And please: don't top post. It makes it easier for
    everybody to add a comment and still maintain a little
    bit of context and keep the thread readable at the same time.
    Thank you.

    --
    Karl Heinz Buchegger
     
    Karl Heinz Buchegger, Oct 20, 2003
    #15
  16. solartimba

    red floyd Guest

    Re: Why can't a C++ string be used as the path name to open a filebut a C-string can?

    Karl Heinz Buchegger wrote:
    >
    > Gregory Smith wrote:
    >
    >>Try using
    >>string path("c:\test.txt");
    >> ifstream infile(path.c_str());
    >>

    >
    >
    > Note:
    >
    > string path( "c:\\test.txt" );
    >
    > And please: don't top post. It makes it easier for
    > everybody to add a comment and still maintain a little
    > bit of context and keep the thread readable at the same time.
    > Thank you.
    >


    However, I believe it's a deficiency in the iostream library that the constructors
    and open() calls for [io]fstreams don't allow std::strings to be used directly.
    Though I guess that that one's really for comp.std.c++.....
     
    red floyd, Oct 20, 2003
    #16
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Mr. SweatyFinger

    why why why why why

    Mr. SweatyFinger, Nov 28, 2006, in forum: ASP .Net
    Replies:
    4
    Views:
    921
    Mark Rae
    Dec 21, 2006
  2. Mr. SweatyFinger
    Replies:
    2
    Views:
    2,072
    Smokey Grindel
    Dec 2, 2006
  3. Travis
    Replies:
    6
    Views:
    549
    Markus Schoder
    Jun 28, 2007
  4. Casey Hawthorne
    Replies:
    1
    Views:
    728
    Arne Vajhøj
    Mar 18, 2009
  5. Mike Schilling
    Replies:
    2
    Views:
    582
    Arne Vajhøj
    Mar 5, 2010
Loading...

Share This Page