473,413 Members | 1,733 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,413 software developers and data experts.

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

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
15 17528

"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

"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
"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
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
"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
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
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

"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
> 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
"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
"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

"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
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


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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

20
by: CHIN | last post by:
Hi all.. here s my problem ( maybe some of you saw me on other groups, but i cant find the solution !! ) I have to upload a file to an external site, so, i made a .vbs file , that logins to...
6
by: Charles Morrall | last post by:
I have no experience with DB2 as such, but I've been tasked with configuring backup of a server running DB2 v8 on Windows Server 2003. I do have some experience with backups in general though. The...
4
by: Tony Maresca | last post by:
Is there any framework function or code floating around that someone knows of, that can take a long path name and remove the middle parts to make it fit in a given field of characters for display...
0
by: ungvichian | last post by:
I am trying to write a program in VC++.Net that will read a text file with wireframe data in it and display the wireframe it represents. This link has a sample of the code I'm basing my program off...
0
by: Me | last post by:
I have an application that has a text box. At the end on the text box is the standard elypsis (command button) for launching the Open File Dialog box. I want the user to select a certain executable...
0
by: snabakvinod | last post by:
Hi, In VC8 project release mode, using __FILE__ macro and compiler option /FC, I can get the full path name with all small letters(Ex: c:\testvc8project\samplefilename.cpp). But I could...
0
by: Vinod | last post by:
Hi, In VC8 project release mode, using __FILE__ macro and compiler option /FC, I can get the full path name with all small letters(Ex: c:\testvc8project\samplefilename.cpp). But I could not...
6
by: Lubomir | last post by:
Hi, Where in .NET are definded constants for maximal file name length and maximal file path? Thanks, Lubomir
0
by: Rinoa | last post by:
vb6 Vista I'm attempting to get the file path returned by an open file dialog so that I can put it in the text property of a text box. However, I have no idea what the ofd property would be. Help?
4
by: ish | last post by:
hello everybody...... plz help me out... how we can store the acual path of uploading file.i want to store the path of file in my database...bt i m not getting anyway to implement that......if...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.