By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,853 Members | 1,034 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,853 IT Pros & Developers. It's quick & easy.

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

P: n/a
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??)?
Jul 19 '05 #1
Share this Question
Share on Google+
15 Replies


P: n/a

"solartimba" <ka*****@hotmail.com> wrote in message
news:1a**************************@posting.google.c 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());
Jul 19 '05 #2

P: n/a

"solartimba" <ka*****@hotmail.com> wrote in message
news:1a**************************@posting.google.c 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
Jul 19 '05 #3

P: n/a
"solartimba" <ka*****@hotmail.com> wrote in message
news:1a**************************@posting.google.c 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/
Jul 19 '05 #4

P: n/a
Cy Edmunds wrote:
"solartimba" <ka*****@hotmail.com> wrote in message
news:1a**************************@posting.google.c 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.

Jul 19 '05 #5

P: n/a
"Cy Edmunds" <ce******@spamless.rochester.rr.com> wrote in message
news:jf*******************@twister.nyroc.rr.com...
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
Jul 19 '05 #6

P: n/a
In article <jf*******************@twister.nyroc.rr.com>,
Cy Edmunds <ce******@spamless.rochester.rr.com> 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
Jul 19 '05 #7

P: n/a
Mike Wahler wrote in
news:FE***************@newsread3.news.pas.earthlin k.net:
"Cy Edmunds" <ce******@spamless.rochester.rr.com> wrote in message
news:jf*******************@twister.nyroc.rr.com...
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::operator 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/
Jul 19 '05 #8

P: n/a

"Rob Williscroft" <rt*@freenet.REMOVE.co.uk> wrote in message
news:Xn**********************************@195.129. 110.130...
Mike Wahler wrote in
news:FE***************@newsread3.news.pas.earthlin k.net:
"Cy Edmunds" <ce******@spamless.rochester.rr.com> wrote in message
news:jf*******************@twister.nyroc.rr.com...
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::operator 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
Jul 19 '05 #9

P: n/a
> 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.
Jul 19 '05 #10

P: n/a
"Mike Wahler" <mk******@mkwahler.net> wrote in message
news:yq***************@newsread4.news.pas.earthlin k.net...

"Rob Williscroft" <rt*@freenet.REMOVE.co.uk> wrote in message
news:Xn**********************************@195.129. 110.130...
Mike Wahler wrote in
news:FE***************@newsread3.news.pas.earthlin k.net:
"Cy Edmunds" <ce******@spamless.rochester.rr.com> wrote in message
news:jf*******************@twister.nyroc.rr.com...
> 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::operator 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/
Jul 19 '05 #11

P: n/a
"Andrew Koenig" <ar*@acm.org> wrote in message
news:38***********************@bgtnsc04-news.ops.worldnet.att.net...
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/
Jul 19 '05 #12

P: n/a

"Cy Edmunds" <ce******@spamless.rochester.rr.com> wrote in message news:w%***************@twister.nyroc.rr.com...
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).
Jul 19 '05 #13

P: n/a
Try using
string path("c:\test.txt");
ifstream infile(path.c_str());

"solartimba" <ka*****@hotmail.com> wrote in message
news:1a**************************@posting.google.c 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??)?

Jul 19 '05 #14

P: n/a


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
kb******@gascad.at
Jul 19 '05 #15

P: n/a
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++.....

Jul 19 '05 #16

This discussion thread is closed

Replies have been disabled for this discussion.