I want to use an argument of type *char and an argument of type int
(indicating the length of the character array) for the read method of
ifstream.

However, the length of my character array is variable.

So I am declaring
vector<char> chars

But I cannot use this as the first argument of read.

How can I accomplish using a variable length first argument?

Re: how to read variable length block of data with ifstream by Igor

Igor
Thu May 31 22:56:12 CDT 2007

"Ananya" <Ananya@discussions.microsoft.com> wrote in message
news:A6CF1795-D246-412B-8835-3F858F4287A3@microsoft.com
> I want to use an argument of type *char and an argument of type int
> (indicating the length of the character array) for the read method of
> ifstream.
>
> However, the length of my character array is variable.
>
> So I am declaring
> vector<char> chars
>
> But I cannot use this as the first argument of read.

vector<char> chars;
int size = ...;
chars.resize(size);

ifstream ifs = ...;
ifs.read(&chars.front(), size);

--
With best wishes,
Igor Tandetnik

With sufficient thrust, pigs fly just fine. However, this is not
necessarily a good idea. It is hard to be sure where they are going to
land, and it could be dangerous sitting under them as they fly
overhead. -- RFC 1925



Re: how to read variable length block of data with ifstream by Ananya

Ananya
Fri Jun 01 03:42:00 CDT 2007

Wow, this works perfectly! Thanks lot!
It's amazing that I could not find this answer in any of my C++ books.
Do you know of any C++ book that does contain this answer?


"Igor Tandetnik" wrote:

> "Ananya" <Ananya@discussions.microsoft.com> wrote in message
> news:A6CF1795-D246-412B-8835-3F858F4287A3@microsoft.com
> > I want to use an argument of type *char and an argument of type int
> > (indicating the length of the character array) for the read method of
> > ifstream.
> >
> > However, the length of my character array is variable.
> >
> > So I am declaring
> > vector<char> chars
> >
> > But I cannot use this as the first argument of read.
>
> vector<char> chars;
> int size = ...;
> chars.resize(size);
>
> ifstream ifs = ...;
> ifs.read(&chars.front(), size);
>
> --
> With best wishes,
> Igor Tandetnik
>
> With sufficient thrust, pigs fly just fine. However, this is not
> necessarily a good idea. It is hard to be sure where they are going to
> land, and it could be dangerous sitting under them as they fly
> overhead. -- RFC 1925
>
>
>

Re: how to read variable length block of data with ifstream by Stefan

Stefan
Fri Jun 01 05:08:20 CDT 2007

On 6/1/2007 10:42 AM, Ananya wrote:
>
> "Igor Tandetnik" wrote:
>
>> "Ananya" <Ananya@discussions.microsoft.com> wrote in message
>> news:A6CF1795-D246-412B-8835-3F858F4287A3@microsoft.com
>>> I want to use an argument of type *char and an argument of type int
>>> (indicating the length of the character array) for the read method of
>>> ifstream.
>>>
>>> However, the length of my character array is variable.
>>>
>>> So I am declaring
>>> vector<char> chars
>>>
>>> But I cannot use this as the first argument of read.
>> vector<char> chars;
>> int size = ...;
>> chars.resize(size);
>>
>> ifstream ifs = ...;
>> ifs.read(&chars.front(), size);
> Wow, this works perfectly! Thanks lot!
> It's amazing that I could not find this answer in any of my C++ books.
> Do you know of any C++ book that does contain this answer?
>

It's a little bit hidden in Scott Meyers' "Effective STL", Item 16:
"Know how to pass vector and string data to legacy APIs"

Regards,
Stefan
--
Stefan Naewe stefan dot naewe at atlas-elektronik dot com
Don't top-post http://www.catb.org/~esr/jargon/html/T/top-post.html
Plain text mails only, please http://www.expita.com/nomime.html