iostream - BYTE array

Discussion in 'C++' started by Joy Maitland, Nov 19, 2008.

  1. Joy Maitland

    Joy Maitland Guest

    how to use <iostream> to read a file to a const BYTE array?

    const BYTE *pbBinary
    Joy Maitland, Nov 19, 2008
    #1
    1. Advertising

  2. On Nov 19, 9:13 am, Joy Maitland <> wrote:
    > how to use <iostream> to read a file to a const BYTE array?
    >
    > const BYTE *pbBinary


    You don't normally read a file into a pointer. You read a file into an
    array and point that pointer to that array:

    #include <iostream>
    #include <vector>
    #include <iterator>

    int main()
    {
    typedef unsigned char BYTE;
    // read from std::cin
    std::vector<BYTE> bytes(
    (std::istreambuf_iterator<char>(std::cin))
    , (std::istreambuf_iterator<char>())
    );
    if(bytes.empty())
    ; // no bytes have been read

    BYTE const* pbBinary = &bytes[0];
    }

    --
    Max
    Maxim Yegorushkin, Nov 19, 2008
    #2
    1. Advertising

  3. Joy Maitland

    Joy Maitland Guest

    why when i change the code from std::cin to read from file
    c:/test2.xml I got error

    =======================
    typedef unsigned char BYTE;
    std::eek:fstream file1("c:/test2.xml");

    // read from std::cin
    std::vector<BYTE> bytes(
    (std::istreambuf_iterator<char>(file1))
    , (std::istreambuf_iterator<char>())
    );
    if(bytes.empty())
    ; // no bytes have been read

    BYTE const* pbBinary = &bytes[0];

    ===========================

    1>d:\snd\remote\remotedemo\app\virtualawear\main.cpp(31) : error
    C2440: '<function-style-cast>' : cannot convert from 'std::eek:fstream'
    to 'std::istreambuf_iterator<_Elem,_Traits>'
    1> with
    1> [
    1> _Elem=char,
    1> _Traits=std::char_traits<char>
    1> ]
    1> No constructor could take the source type, or constructor
    overload resolution was ambiguous

    ============================






    On Wed, 19 Nov 2008 01:22:44 -0800 (PST), Maxim Yegorushkin
    <> wrote:

    >On Nov 19, 9:13?am, Joy Maitland <> wrote:
    >> how to use <iostream> to read a file to a const BYTE array?
    >>
    >> const BYTE *pbBinary

    >
    >You don't normally read a file into a pointer. You read a file into an
    >array and point that pointer to that array:
    >
    > #include <iostream>
    > #include <vector>
    > #include <iterator>
    >
    > int main()
    > {
    > typedef unsigned char BYTE;
    > // read from std::cin
    > std::vector<BYTE> bytes(
    > (std::istreambuf_iterator<char>(std::cin))
    > , (std::istreambuf_iterator<char>())
    > );
    > if(bytes.empty())
    > ; // no bytes have been read
    >
    > BYTE const* pbBinary = &bytes[0];
    > }
    Joy Maitland, Nov 19, 2008
    #3
  4. On Nov 19, 9:36 am, Joy Maitland <> wrote:
    > why when i change the code from std::cin to read from file
    > c:/test2.xml  I got error
    >
    > =======================
    > typedef unsigned char BYTE;
    >                 std::eek:fstream file1("c:/test2.xml");


    Becuase you are using an output stream, rather than an input. Change
    ofstream to ifstream.

    --
    Max
    Maxim Yegorushkin, Nov 19, 2008
    #4
    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. Kirby
    Replies:
    3
    Views:
    631
    Kirby
    Oct 8, 2004
  2. John Tiger
    Replies:
    10
    Views:
    5,565
  3. ai@work
    Replies:
    9
    Views:
    529
    Ron Natalie
    Dec 16, 2004
  4. S. Nurbe

    iostream + iostream.h

    S. Nurbe, Jan 14, 2005, in forum: C++
    Replies:
    7
    Views:
    757
    red floyd
    Jan 15, 2005
  5. red floyd
    Replies:
    3
    Views:
    527
    Dietmar Kuehl
    Mar 8, 2005
Loading...

Share This Page