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

How can I read 2 characters from file into a std::string

P: n/a
How can I read in two characters from a file into a std::string?

the long way:

fstream f("myfile.bin", ios::binary | ios:in); // Opened for read
binary...
unsigned char buffer[3];
buffer[2] = '\0';
f.read(buffer, 2);
std::string x(buffer);

Got a better way?

Jun 6 '07 #1
Share this Question
Share on Google+
6 Replies


P: n/a
SpreadTooThin wrote:
How can I read in two characters from a file into a std::string?

the long way:

fstream f("myfile.bin", ios::binary | ios:in); // Opened for read binary...
unsigned char buffer[3];
buffer[2] = '\0';
f.read(buffer, 2);
std::string x(buffer);

Got a better way?
fstream f("myfile.bin", ios::binary | ios:in);
char buffer[2];
f.read(buffer, 2);
std::string x(buffer, buffer+2);

They do behave differently.
Jun 6 '07 #2

P: n/a
On Jun 6, 10:11 pm, SpreadTooThin <bjobrie...@gmail.comwrote:
How can I read in two characters from a file into a std::string?
the long way:
fstream f("myfile.bin", ios::binary | ios:in); // Opened for read
binary...
unsigned char buffer[3];
buffer[2] = '\0';
f.read(buffer, 2);
std::string x(buffer);
Got a better way?
std::string x ;
x += f.get() ;
x += f.get() ;

(Of course, you'll want to check that you actually did get two
characters.)

Something that will work with all current implementations, and
will be guaranteed by the next version of the standard:

std::string x( 2, ' ' ) ;
f.read( &x[ 0 ], 2 ) ;

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

Jun 7 '07 #3

P: n/a
James Kanze wrote:
....
Something that will work with all current implementations, and
will be guaranteed by the next version of the standard:
Do you have a reference to the this proposal (contiguous strings)?
Jun 7 '07 #4

P: n/a
On 2007-06-07 01:59:03 -0700, Gianni Mariani <gi*******@mariani.wssaid:
James Kanze wrote:
...
>Something that will work with all current implementations, and
will be guaranteed by the next version of the standard:

Do you have a reference to the this proposal (contiguous strings)?
See: http://www.open-std.org/jtc1/sc22/wg...fects.html#530

--
Clark S. Cox III
cl*******@gmail.com

Jun 7 '07 #5

P: n/a
On Jun 7, 10:59 am, Gianni Mariani <gi3nos...@mariani.wswrote:
James Kanze wrote:
...
Something that will work with all current implementations, and
will be guaranteed by the next version of the standard:
Do you have a reference to the this proposal (contiguous strings)?
It's in the current draft (N2134): "The char-like objects in a
basic_string object shall be stored contiguously. That is, for
any basic_string object s, the identity &*(s.begin() + n) ==
&*s.begin() + n shall hold for all values of n such that 0 <= n
< s.size()." From what I understand, a non-const version of
data() will also be added (as well as const and non-const data()
functions to std::vector), in order to obtain the address in a
way somewhat more transparently than &s[0].

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

Jun 8 '07 #6

P: n/a
James Kanze wrote:
>
It's in the current draft (N2134)
Just so everyone's up to date, the current draft (reflecting changes
made at the April, 2007 meeting) is N2284.

--

-- Pete
Roundhouse Consulting, Ltd. (www.versatilecoding.com)
Author of "The Standard C++ Library Extensions: a Tutorial and
Reference." (www.petebecker.com/tr1book)
Jun 8 '07 #7

This discussion thread is closed

Replies have been disabled for this discussion.