append data to a vector

Discussion in 'C++' started by mosfet, Sep 28, 2007.

  1. mosfet

    mosfet Guest

    Hi,

    I am trying to rewrite a c++ class wrapping wininet (windows ftp/http
    lib) functions.
    I do the folllowing to get the response :




    while ((return_code = InternetQueryDataAvailable(hReq,
    &bytes_available,0, 0) != 0) &&
    bytes_available > 0)
    {
    vector<char> response_buffer(bytes_available);
    DWORD size_read;

    if ((return_code = InternetReadFile(hReq, &response_buffer[0],
    bytes_available, &size_read) != 0) &&
    size_read > 0)
    {
    total_read += size_read;
    m_RawData += response_buffer; //m_RawData is also a vector<char>

    }
    else
    {
    break;
    }
    }


    As you can see I store data read in vector response_buffer and I would
    like to append it to my global buffer so I tried :

    m_RawData += response_buffer; but it 's not possible...
    How can I do it ?
    mosfet, Sep 28, 2007
    #1
    1. Advertising

  2. mosfet wrote:
    > Hi,
    >
    > I am trying to rewrite a c++ class wrapping wininet (windows ftp/http
    > lib) functions.
    > I do the folllowing to get the response :
    >
    >
    >
    >
    > while ((return_code = InternetQueryDataAvailable(hReq,
    > &bytes_available,0, 0) != 0) &&
    > bytes_available > 0)
    > {
    > vector<char> response_buffer(bytes_available);
    > DWORD size_read;
    >
    > if ((return_code = InternetReadFile(hReq, &response_buffer[0],
    > bytes_available, &size_read) != 0) &&
    > size_read > 0)
    > {
    > total_read += size_read;
    > m_RawData += response_buffer; //m_RawData is also a vector<char>
    >
    > }
    > else
    > {
    > break;
    > }
    > }
    >
    >
    > As you can see I store data read in vector response_buffer and I would
    > like to append it to my global buffer so I tried :
    >
    > m_RawData += response_buffer; but it 's not possible...
    > How can I do it ?


    What makes you think that += is defined for vector<char>? Have you
    tried RTFM? It helps, you know.

    Try

    m_RawData.insert(m_RawData.end(),
    response_buffer.begin(), response_buffer.end());

    V
    --
    Please remove capital 'A's when replying by e-mail
    I do not respond to top-posted replies, please don't ask
    Victor Bazarov, Sep 28, 2007
    #2
    1. Advertising

  3. On 2007-09-28 15:18, mosfet wrote:
    > Hi,
    >
    > I am trying to rewrite a c++ class wrapping wininet (windows ftp/http
    > lib) functions.
    > I do the folllowing to get the response :
    >
    >
    >
    >
    > while ((return_code = InternetQueryDataAvailable(hReq,
    > &bytes_available,0, 0) != 0) &&
    > bytes_available > 0)
    > {
    > vector<char> response_buffer(bytes_available);
    > DWORD size_read;
    >
    > if ((return_code = InternetReadFile(hReq, &response_buffer[0],
    > bytes_available, &size_read) != 0) &&
    > size_read > 0)
    > {
    > total_read += size_read;
    > m_RawData += response_buffer; //m_RawData is also a vector<char>
    >
    > }
    > else
    > {
    > break;
    > }
    > }
    >
    >
    > As you can see I store data read in vector response_buffer and I would
    > like to append it to my global buffer so I tried :
    >
    > m_RawData += response_buffer; but it 's not possible...
    > How can I do it ?



    m_RawData.insert(response_buffer.begin(), response_buffer.end(),
    m_RawData.end());

    This will insert all elements in response_buffer at the end of
    m_RawData. Notice that if response_buffer is large it you might gain
    some speed by doing

    m_RawData.reserve(m_RawData.size + response_Buffer.size());

    before inserting.

    --
    Erik Wikström
    =?UTF-8?B?RXJpayBXaWtzdHLDtm0=?=, Sep 28, 2007
    #3
  4. Erik Wikström wrote:
    > [..]


    Couple of nits:

    > m_RawData.insert(response_buffer.begin(), response_buffer.end(),
    > m_RawData.end());


    m_RawData.end() has to be the first argument. The trouble is, if the
    two vectors are of the same type, this will even compile, but most
    likely will have undefined behaviour.

    >
    > This will insert all elements in response_buffer at the end of
    > m_RawData. Notice that if response_buffer is large it you might gain
    > some speed by doing
    >
    > m_RawData.reserve(m_RawData.size + response_Buffer.size());


    m_RawData.reserve(m_RawData.size() + ..

    (size is a function, needs the argument list)

    >
    > before inserting.


    V
    --
    Please remove capital 'A's when replying by e-mail
    I do not respond to top-posted replies, please don't ask
    Victor Bazarov, Sep 28, 2007
    #4
  5. On 2007-09-28 16:11, Victor Bazarov wrote:
    > Erik Wikstr�m wrote:
    >> [..]

    >
    > Couple of nits:
    >
    >> m_RawData.insert(response_buffer.begin(), response_buffer.end(),
    >> m_RawData.end());

    >
    > m_RawData.end() has to be the first argument. The trouble is, if the
    > two vectors are of the same type, this will even compile, but most
    > likely will have undefined behaviour.


    Yes, the last two iterators must not be iterators into the container on
    which insert was called.

    --
    Erik Wikström
    =?UTF-8?B?RXJpayBXaWtzdHLDtm0=?=, Sep 28, 2007
    #5
  6. mosfet

    duane hebert Guest

    "mosfet" <> wrote in message
    news:46fcff2f$0$27788$...

    > while ((return_code = InternetQueryDataAvailable(hReq, &bytes_available,0,
    > 0) != 0) &&
    > bytes_available > 0)
    > {
    > vector<char> response_buffer(bytes_available);
    > DWORD size_read;
    >
    > if ((return_code = InternetReadFile(hReq, &response_buffer[0],
    > bytes_available, &size_read) != 0) &&
    > size_read > 0)
    > {
    > total_read += size_read;
    > m_RawData += response_buffer; //m_RawData is also a vector<char>
    >
    > }
    > else
    > {
    > break;
    > }
    > }
    >
    >
    > As you can see I store data read in vector response_buffer and I would
    > like to append it to my global buffer so I tried :
    >
    > m_RawData += response_buffer; but it 's not possible...
    > How can I do it ?


    What type is m_RawData?
    duane hebert, Sep 28, 2007
    #6
  7. mosfet

    Howard Guest

    "duane hebert" <> wrote in message
    news:Hy8Li.33986$...
    >
    > "mosfet" <> wrote in message
    > news:46fcff2f$0$27788$...
    >
    >> while ((return_code = InternetQueryDataAvailable(hReq,
    >> &bytes_available,0, 0) != 0) &&
    >> bytes_available > 0)
    >> {
    >> vector<char> response_buffer(bytes_available);
    >> DWORD size_read;
    >>
    >> if ((return_code = InternetReadFile(hReq, &response_buffer[0],
    >> bytes_available, &size_read) != 0) &&
    >> size_read > 0)
    >> {
    >> total_read += size_read;
    >> m_RawData += response_buffer; //m_RawData is also a vector<char>
    >>
    >> }
    >> else
    >> {
    >> break;
    >> }
    >> }
    >>
    >>
    >> As you can see I store data read in vector response_buffer and I would
    >> like to append it to my global buffer so I tried :
    >>
    >> m_RawData += response_buffer; but it 's not possible...
    >> How can I do it ?

    >
    > What type is m_RawData?
    >


    You might want to read the whole post again... especially where it tells you
    what type that is. :)

    -Howard
    Howard, Sep 29, 2007
    #7
  8. mosfet

    duane hebert Guest

    >>> As you can see I store data read in vector response_buffer and I would
    >>> like to append it to my global buffer so I tried :
    >>>
    >>> m_RawData += response_buffer; but it 's not possible...
    >>> How can I do it ?

    >>
    >> What type is m_RawData?
    >>

    >
    > You might want to read the whole post again... especially where it tells
    > you what type that is. :)


    From the beginning of this thread, all that I see is the line
    above referring to it as "my global buffer" but I don't
    see it mentioned that it's also a vector or whatever.
    duane hebert, Oct 1, 2007
    #8
  9. mosfet

    Howard Guest

    "duane hebert" <> wrote in message
    news:hQ8Mi.14747$...
    >>>> As you can see I store data read in vector response_buffer and I would
    >>>> like to append it to my global buffer so I tried :
    >>>>
    >>>> m_RawData += response_buffer; but it 's not possible...
    >>>> How can I do it ?
    >>>
    >>> What type is m_RawData?
    >>>

    >>
    >> You might want to read the whole post again... especially where it tells
    >> you what type that is. :)

    >
    > From the beginning of this thread, all that I see is the line
    > above referring to it as "my global buffer" but I don't
    > see it mentioned that it's also a vector or whatever.
    >
    >


    in the code, almost at the end...

    >> m_RawData += response_buffer; //m_RawData is also a vector<char>
    Howard, Oct 1, 2007
    #9
  10. mosfet

    Howard Guest

    "duane hebert" <> wrote in message
    news:hQ8Mi.14747$...
    >>>> As you can see I store data read in vector response_buffer and I would
    >>>> like to append it to my global buffer so I tried :
    >>>>
    >>>> m_RawData += response_buffer; but it 's not possible...
    >>>> How can I do it ?
    >>>
    >>> What type is m_RawData?
    >>>

    >>
    >> You might want to read the whole post again... especially where it tells
    >> you what type that is. :)

    >
    > From the beginning of this thread, all that I see is the line
    > above referring to it as "my global buffer" but I don't
    > see it mentioned that it's also a vector or whatever.
    >


    It's in a comment, right about the middle of the post:
    > m_RawData += response_buffer; //m_RawData is also a vector<char>


    -Howard
    Howard, Nov 15, 2007
    #10
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. pmatos
    Replies:
    6
    Views:
    23,789
  2. Replies:
    8
    Views:
    1,915
    Csaba
    Feb 18, 2006
  3. Javier
    Replies:
    2
    Views:
    561
    James Kanze
    Sep 4, 2007
  4. HYRY
    Replies:
    10
    Views:
    604
    Bruno Desthuilliers
    Sep 26, 2007
  5. Rushikesh Joshi
    Replies:
    0
    Views:
    359
    Rushikesh Joshi
    Jul 10, 2004
Loading...

Share This Page