472,989 Members | 2,833 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

Why do so many new style ansi streams and files etc, still use old style strings?

Kza
Hi, just in the process of maintaining some software that used some
funy old string library and char*s , and we are updating everything to
use std::strings. (or should I say std::basic_string<>s)

I find it wierd that that all the new c++ ansi style librarys like the
streams and file handling classes still expect us to use old style
char* type strings. For example, ofstreams open function expects the
filename as a char* parameter rather than a std::string, and the
streams redirection operator << doesnt even work with std::strings but
does with char*s.

I would have expected the opposite, (or perhaps both types of strings
are supported), in that std::strings are the native type of string for
these new c++ style libs.

Seems a bit like a case of not practicing what they are preaching "you
should all use these new style strings, er unless of course you also
need to use our other classes which still only support old style char*
strings"
I know its easy to solve with .c_str() but is there a reason for this?

Mar 3 '06 #1
4 1975
Kza wrote:
Hi, just in the process of maintaining some software that used some
funy old string library and char*s , and we are updating everything to
use std::strings. (or should I say std::basic_string<>s)

I find it wierd that that all the new c++ ansi style librarys like the
streams and file handling classes still expect us to use old style
char* type strings. For example, ofstreams open function expects the
filename as a char* parameter rather than a std::string, and the
streams redirection operator << doesnt even work with std::strings but
does with char*s.
Don't know which library you're using, but the standard requires a
stream inserter for basic_string. If it's really not there, then the
library doesn't conform.

I would have expected the opposite, (or perhaps both types of strings
are supported), in that std::strings are the native type of string for
these new c++ style libs.


const char *str = "abcd";
const string str1("abcd");

The first generates less code and uses less memory. Some people still
care about those things.

There is, though, a proposal to add functions taking string arguments in
the 17 (if I remember correctly) places where there is just a char* version.

--

Pete Becker
Roundhouse Consulting, Ltd.
Mar 3 '06 #2
Kza wrote:
I find it wierd that that all the new c++ ansi style librarys like the
streams and file handling classes still expect us to use old style
char* type strings. For example, ofstreams open function expects the
filename as a char* parameter rather than a std::string,
Yes, this is annoyance which came from two distinct sources, neither
of which gives a satisfying reason:
- There was original a certain desire not to couple the IOStreams
library with the string library and possibly there could even have
been a conversion operator from the string class to 'char const*'.
- The IOStreams specification is older than the string specification
and thus could not reasonably use any particular string class. It
was apparently not properly updated.

I think there is a "clean-up" proposal for various places like this
at the committee which would be expected to make it into TR2 and
into C++0x.
and the
streams redirection operator << doesnt even work with std::strings but
does with char*s.
I'm not sure what you mean: there are both input and output operators
taking 'std::string's as well as a special 'std::getline()' function
also taking strings. These functions are just not members of the
IOStream classes, nor should they: they are namespace level functions
like the counterparts for the respective character type of the
stream.
Seems a bit like a case of not practicing what they are preaching "you
should all use these new style strings, er unless of course you also
need to use our other classes which still only support old style char*
strings"


This is true to some degree for the 'open()' methods in the various
file stream classes. However, I would consider this to be a historical
accident which happened while the people in the committee were busy
getting other stuff right.
--
<mailto:di***********@yahoo.com> <http://www.dietmar-kuehl.de/>
<http://www.eai-systems.com> - Efficient Artificial Intelligence
Mar 3 '06 #3
Pete Becker wrote:
const char *str = "abcd";
const string str1("abcd");

The first generates less code and uses less memory. Some people still
care about those things.


The first probably generates no code at all. The second needs to construct a
string object, which usually includes allocating dynamic memory (and on
destruction giving it back) and copying the string data. On small embedded
systems, this can make a significant difference, and C++ is supposed to be
useful on such systems, too.

Mar 3 '06 #4
"Kza" <kz****@gmail.com> wrote in message
news:11*********************@u72g2000cwu.googlegro ups.com...
I find it wierd that that all the new c++ ansi style librarys like the
streams and file handling classes still expect us to use old style
char* type strings.


It is weird, but there is a reason for it: When the committee talked about
the possibility of using std::string values to represent file names, the
delegation from Japan requested that the committee consider only proposals
that addressed both narrow and wide strings. In effect, they were asking
"If you expand the standard to allow file names to be strings of European
characters, we want it to allow strings of Japanese characters at the same
time." This was an entirely reasonable request under the circumstances, and
in response, the committee agreed not to define a mapping between strings
and file names in the current standard.
Mar 3 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: aa | last post by:
Is it OK to include an ANSI file into a UTF-8 file?
3
by: Scott Brady Drummonds | last post by:
Hello, all, My most recent assignment has me working on a medium- to large-sized Windows-based C++ software project. My background is entirely on UNIX systems, where it appears that most of my...
8
by: Dave Moore | last post by:
I realize this is a somewhat platform specific question, but I think it is still of general enough interest to ask it here ... if I am wrong I guess I will find out 8*). As we all know, DOS uses...
8
by: bonj | last post by:
hello I hope somebody can help me get my head around this area of 'stream' programming... I know that streams are very fashionable nowadays so hopefully there'll be lots of replies. ;-) ...
6
by: dndfan | last post by:
Hello, In the short time I have spent reading this newsgroup, I have seen this sort of declaration a few times: > int > func (string, number, structure) > char* string > int number >...
1
by: Daniel | last post by:
Does system.xml have any way to transofrm data with an xswl style sheet using strings like MSXML2 does? how to convert this to use System.XML so i do not depend on MSXML2 interop? static...
7
by: Paul Connolly | last post by:
char *s = "Hello"; s = 'J'; puts(s); might print "Jello" in a pre-ANSI compiler - is the behaviour of this program undefined in any pre-ANSI compiler - or would it always have printed "Jello"...
0
by: satish | last post by:
Hi, I have an issue ..i have a text which has the information of 4 test files and they are placed in the order 4 --- this number says (total no of text files) File1 1370 -- Memory of the text...
2
by: =?Utf-8?B?S2VsdmluIEpvaG5zb24=?= | last post by:
I am wrapping a legacy C api using C++\CLI. I will use the resulting classes from C# and VB.net. I need to pass command line arguments from the .net languages to the c api functions, so I've...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: Aliciasmith | last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
0
tracyyun
by: tracyyun | last post by:
Hello everyone, I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
4
NeoPa
by: NeoPa | last post by:
Hello everyone. I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report). I know it can be done by selecting :...
1
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
3
by: nia12 | last post by:
Hi there, I am very new to Access so apologies if any of this is obvious/not clear. I am creating a data collection tool for health care employees to complete. It consists of a number of...
4
by: GKJR | last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...
3
SueHopson
by: SueHopson | last post by:
Hi All, I'm trying to create a single code (run off a button that calls the Private Sub) for our parts list report that will allow the user to filter by either/both PartVendor and PartType. On...

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.