If I have a char[] buffer and I load some data in it by
using _read or something similar, and the buffer is bigger
than the data, how do I copy only the valid data to
another buffer or trim the buffer?
Thanks

Re: char[] by Kobi

Kobi
Tue Jul 29 09:55:35 CDT 2003

Mark,
> If I have a char[] buffer and I load some data in it by
> using _read or something similar, and the buffer is bigger
> than the data, how do I copy only the valid data to

Declare variable called nDataSize which will hold the size of data that has
been read, and when copying data for example with memcpy you will pass it as
a parameter.

> another buffer or trim the buffer?
You can't trim buffer that was allocated on stack. You can allocate data
dynamically with "malloc" and then 'trim' it using "realloc". But usually
you should know the needed size before allocating, and allocate only needed
size with new/malloc.
--
Regards,
Kobi Ben Tzvi


"Mark" <caveno@cs.com> wrote in message
news:095401c355d4$09b747e0$a601280a@phx.gbl...
> If I have a char[] buffer and I load some data in it by
> using _read or something similar, and the buffer is bigger
> than the data, how do I copy only the valid data to
> another buffer or trim the buffer?
> Thanks



RE: char[] by ahoskins

ahoskins
Tue Jul 29 13:32:44 CDT 2003


> From: "Mark" <caveno@cs.com>
> Sender: "Mark" <caveno@cs.com>
> Message-ID: <095401c355d4$09b747e0$a601280a@phx.gbl>
>
> If I have a char[] buffer and I load some data in it by
> using _read or something similar, and the buffer is bigger
> than the data, how do I copy only the valid data to
> another buffer or trim the buffer?
> Thanks
>

_read() returns the number of bytes read. If the data is a string you
should zero-terminate the buffer by doing
int cnt = _read(...)
if ( cnt >= 0 )
*(buffer + cnt) = '\0';

Now you can use strcpy() which reads as string until it gets to a 0 char.

If it's other data you can use the method mentioned before.

--
Drew Hoskins, Visual C++ Team
This posting is provided AS IS with no warranties, and confers no rights.


Re: char[] by danderson

danderson
Tue Jul 29 14:53:31 CDT 2003

"Kobi Ben Tzvi" <tsumbush@hotmail.comREMOVETHIS> wrote in message news:<utML5FeVDHA.1680@tk2msftngp13.phx.gbl>...
> Mark,
> > If I have a char[] buffer and I load some data in it by
> > using _read or something similar, and the buffer is bigger
> > than the data, how do I copy only the valid data to
>
> Declare variable called nDataSize which will hold the size of data that has
> been read, and when copying data for example with memcpy you will pass it as
> a parameter.
>
> > another buffer or trim the buffer?
> You can't trim buffer that was allocated on stack. You can allocate data
> dynamically with "malloc" and then 'trim' it using "realloc". But usually
> you should know the needed size before allocating, and allocate only needed
> size with new/malloc.
> --
> Regards,
> Kobi Ben Tzvi
>
>
> "Mark" <caveno@cs.com> wrote in message
> news:095401c355d4$09b747e0$a601280a@phx.gbl...
> > If I have a char[] buffer and I load some data in it by
> > using _read or something similar, and the buffer is bigger
> > than the data, how do I copy only the valid data to
> > another buffer or trim the buffer?
> > Thanks

Even better, if you are doing C++, do not use char[]/malloc/realloc,
uses vector<char>

Danderson