Quickest way of reading a binary file into a vector<char>?

Discussion in 'C++' started by me, Nov 11, 2004.

  1. me

    me Guest

    Hi guys - the question is in the subject line.

    I thought of one quick way:

    std::ifstream input("myfile.dat") ;

    std::istreambuf_iterator beg(input), end ;

    std::vector DataFile(beg,end) ;

    But Visual Studio complains about not being able to convert param 1 to
    unsigned int....

    Shame, as it seems really neat....

    So is there a 'normal' way of doing it?

    Cheers

    pjw
    me, Nov 11, 2004
    #1
    1. Advertising

  2. me wrote:
    > Hi guys - the question is in the subject line.
    >
    > I thought of one quick way:
    >
    > std::ifstream input("myfile.dat") ;
    >
    > std::istreambuf_iterator beg(input), end ;
    >
    > std::vector DataFile(beg,end) ;
    >
    > But Visual Studio complains about not being able to convert param 1 to
    > unsigned int....
    >
    > Shame, as it seems really neat....
    >
    > So is there a 'normal' way of doing it?


    If you know the size of the file (and you probably can figure it out),
    then

    std::vector<char> DataFile(filesize);
    input.read(&DataFile[0], filesize);

    V
    Victor Bazarov, Nov 11, 2004
    #2
    1. Advertising

  3. me

    me Guest

    Victor Bazarov wrote:
    > me wrote:
    >
    >> Hi guys - the question is in the subject line.
    >>
    >> I thought of one quick way:
    >>
    >> std::ifstream input("myfile.dat") ;
    >>
    >> std::istreambuf_iterator beg(input), end ;
    >>
    >> std::vector DataFile(beg,end) ;
    >>
    >> But Visual Studio complains about not being able to convert param 1 to
    >> unsigned int....
    >>
    >> Shame, as it seems really neat....
    >>
    >> So is there a 'normal' way of doing it?

    >
    >
    > If you know the size of the file (and you probably can figure it out),
    > then
    >
    > std::vector<char> DataFile(filesize);
    > input.read(&DataFile[0], filesize);
    >
    > V

    Sounds reasonable enough - but to read the file length i need to start
    at the start and read to the end, so I might as well store values as i
    go....
    me, Nov 11, 2004
    #3
  4. me

    Jeff Flinn Guest

    "me" <> wrote in message news:...
    > Hi guys - the question is in the subject line.
    >
    > I thought of one quick way:
    >
    > std::ifstream input("myfile.dat") ;
    >
    > std::istreambuf_iterator beg(input), end ;
    >
    > std::vector DataFile(beg,end) ;


    This is incorrect. What type does your std::vector contain? ie: std::vector<
    ??? >

    > But Visual Studio complains about not being able to convert param 1 to
    > unsigned int....
    >
    > Shame, as it seems really neat....
    >
    > So is there a 'normal' way of doing it?


    Depending on what your doing with the binary data, the 'quickest' way is not
    to read the binary file in at all. In my experience, the 'quickest' is to
    use your platforms facilities to memory map the file. The address and length
    returned can then be used as _begin_ and _end_ iterators.

    Jeff F
    Jeff Flinn, Nov 11, 2004
    #4
  5. me wrote:
    > [..]
    > Sounds reasonable enough - but to read the file length i need to start
    > at the start and read to the end, so I might as well store values as i
    > go....



    If you have the file name, you could use platform-specific means of
    finding out the size of the file on your file system.

    V
    Victor Bazarov, Nov 11, 2004
    #5
  6. me

    Chris Theis Guest

    "me" <> wrote in message news:...
    > Hi guys - the question is in the subject line.
    >
    > I thought of one quick way:
    >
    > std::ifstream input("myfile.dat") ;
    >
    > std::istreambuf_iterator beg(input), end ;
    >
    > std::vector DataFile(beg,end) ;
    >
    > But Visual Studio complains about not being able to convert param 1 to
    > unsigned int....
    >
    > Shame, as it seems really neat....
    >
    > So is there a 'normal' way of doing it?
    >

    [SNIP]

    I´d recommend to follow Victor´s solution for binary files. However, I might
    comment on the problem Visual Studio is complaining about. The vector
    implementation of VC++ 6 is not capable of handling iterators passed in the
    ctor. Hence, you´ll have to use copy() with a back_inserter.

    Cheers
    Chris
    Chris Theis, Nov 11, 2004
    #6
  7. Victor Bazarov wrote:
    > me wrote:
    >
    >> [..]
    >> Sounds reasonable enough - but to read the file length i need to start
    >> at the start and read to the end, so I might as well store values as i
    >> go....

    >
    > If you have the file name, you could use platform-specific means of
    > finding out the size of the file on your file system.
    >
    > V


    'stat' is platform specific, but its part of POSIX - but that makes it
    reasonably standard, right? Unless you're on Windows, in which case
    check MSDN

    --
    http://www.it-is-truth.org/
    Asfand Yar Qazi, Nov 13, 2004
    #7
    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.

Share This Page