Home     |     .Net Programming    |     cSharp Home    |     Sql Server Home    |     Javascript / Client Side Development     |     Ajax Programming

Ruby on Rails Development     |     Perl Programming     |     C Programming Language     |     C++ Programming     |     IT Jobs

Python Programming Language     |     Laptop Suggestions?    |     TCL Scripting     |     Fortran Programming     |     Scheme Programming Language


 
 
Cervo Technologies
The Right Source to Outsource

MS Dynamics CRM 3.0

C++ Programming

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


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?

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.

On Jun 6, 10:11 pm, SpreadTooThin <bjobrie@gmail.com> 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?

    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:james.ka@gmail.com
Conseils en informatique oriente objet/
                   Beratung in objektorientierter Datenverarbeitung
9 place Smard, 78210 St.-Cyr-l'cole, France, +33 (0)1 30 23 00 34

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)?
On 2007-06-07 01:59:03 -0700, Gianni Mariani <gi3nos@mariani.ws> said:

> 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/wg21/docs/lwg-defects.html#530

--
Clark S. Cox III
clarkc@gmail.com

On Jun 7, 10:59 am, Gianni Mariani <gi3nos@mariani.ws> wrote:

> 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:james.ka@gmail.com
Conseils en informatique oriente objet/
                   Beratung in objektorientierter Datenverarbeitung
9 place Smard, 78210 St.-Cyr-l'cole, France, +33 (0)1 30 23 00 34

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)

Add to del.icio.us | Digg this | Stumble it | Powered by Megasolutions Inc