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

S

solartimba

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??)?
 
C

cpp_weenie

solartimba said:
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());
 
M

Mike Wahler

solartimba said:
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
 
C

Cy Edmunds

solartimba said:
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.
 
L

lilburne

Cy said:
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.
 
M

Mike Wahler

Cy Edmunds said:
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
 
J

John Villalovos

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
 
R

Rob Williscroft

Mike Wahler wrote in
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.
 
M

Mike Wahler

Rob Williscroft said:
Mike Wahler wrote in


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
 
A

Andrew Koenig

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.
 
C

Cy Edmunds

Mike Wahler said:
Not that, I think that would indeed be useful.


And I agree with you. :)


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.
 
C

Cy Edmunds

Andrew Koenig said:
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.
 
R

Ron Natalie

Cy Edmunds said:
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).
 
K

Karl Heinz Buchegger

Gregory said:
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.
 
R

red floyd

Karl said:
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++.....
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top