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??)? 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());
"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
"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/
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.
"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
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
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/
"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
> 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.
"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/
"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/
"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).
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??)?
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
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++..... This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
by: Lubomir |
last post by:
Hi,
Where in .NET are definded constants for maximal file name length and
maximal file path?
Thanks,
Lubomir
|
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?
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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,...
|
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...
|
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,...
|
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...
|
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,...
|
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...
|
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...
| |