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

M

me

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
 
V

Victor Bazarov

me said:
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
 
M

me

Victor said:
me said:
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....
 
J

Jeff Flinn

me said:
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
 
V

Victor Bazarov

me said:
[..]
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
 
C

Chris Theis

me said:
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
 
A

Asfand Yar Qazi

Victor said:
me said:
[..]
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,901
Latest member
Noble71S45

Latest Threads

Top