Velocity Reviews - Computer Hardware Reviews

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

Reply
Thread Tools

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

 
 
solartimba
Guest
Posts: n/a
 
      10-17-2003
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??)?
 
Reply With Quote
 
 
 
 
cpp_weenie
Guest
Posts: n/a
 
      10-17-2003

"solartimba" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) om...
> 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());


 
Reply With Quote
 
 
 
 
Mike Wahler
Guest
Posts: n/a
 
      10-17-2003

"solartimba" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) om...
> 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


 
Reply With Quote
 
Cy Edmunds
Guest
Posts: n/a
 
      10-17-2003
"solartimba" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) om...
> 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/


 
Reply With Quote
 
lilburne
Guest
Posts: n/a
 
      10-17-2003
Cy Edmunds wrote:

> "solartimba" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed) om...
>
>>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.

 
Reply With Quote
 
Mike Wahler
Guest
Posts: n/a
 
      10-17-2003
"Cy Edmunds" <(E-Mail Removed)> wrote in message
news:jf_jb.29877$(E-Mail Removed)...
> 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


 
Reply With Quote
 
John Villalovos
Guest
Posts: n/a
 
      10-18-2003
In article <jf_jb.29877$(E-Mail Removed)>,
Cy Edmunds <(E-Mail Removed)> 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
 
Reply With Quote
 
Rob Williscroft
Guest
Posts: n/a
 
      10-18-2003
Mike Wahler wrote in
news:FE_jb.652$(E-Mail Removed) k.net:

> "Cy Edmunds" <(E-Mail Removed)> wrote in message
> news:jf_jb.29877$(E-Mail Removed)...
>> 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: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:ath (std::tr2?) would be
better.

Rob.
--
http://www.victim-prime.dsl.pipex.com/
 
Reply With Quote
 
Mike Wahler
Guest
Posts: n/a
 
      10-18-2003

"Rob Williscroft" <(E-Mail Removed)> wrote in message
news:Xns941810BED101FukcoREMOVEfreenetrtw@195.129. 110.130...
> Mike Wahler wrote in
> news:FE_jb.652$(E-Mail Removed) k.net:
>
> > "Cy Edmunds" <(E-Mail Removed)> wrote in message
> > news:jf_jb.29877$(E-Mail Removed)...
> >> 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: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:ath (std::tr2?) would be
> better.


I'd have to think about that.

Thanks for your input.

-Mike


 
Reply With Quote
 
Andrew Koenig
Guest
Posts: n/a
 
      10-18-2003
> 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.


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
I am buying all of the below Cisco equipment USED OR NEW. NOTE someline items say new or used, but I will buy them in both conditions. network buyer VOIP 0 12-23-2010 01:26 AM
Re: How to get the file name from a String containing the path plus the file name? Mike Schilling Java 2 03-05-2010 03:53 AM
why why why why why Mr. SweatyFinger ASP .Net 4 12-21-2006 01:15 PM
findcontrol("PlaceHolderPrice") why why why why why why why why why why why Mr. SweatyFinger ASP .Net 2 12-02-2006 03:46 PM
INPUT Type="FILE" - Just want to get the file name and path as string kevin@webpageconcepts.com ASP .Net 4 08-02-2006 05:47 PM



Advertisments