473,396 Members | 1,921 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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

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
Jul 22 '05 #1
6 4111
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
Jul 22 '05 #2
me
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....

Jul 22 '05 #3

"me" <me@me.com> wrote in message news:2v*************@uni-berlin.de...
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
Jul 22 '05 #4
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
Jul 22 '05 #5

"me" <me@me.com> wrote in message news:2v*************@uni-berlin.de...
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

Jul 22 '05 #6
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/
Jul 22 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Steven Reddie | last post by:
I have a large char which I'd like to copy a portion of into a vector<char>. I can't seem to locate a simple method for doing so. I assume involving iterators will not result in memcpy not being...
2
by: Chris Thompson | last post by:
Hi I'm writing a p2p client for an existing protocol. I used a std::vector<char> as a buffer for messages read from the server. The message length is the first 4 bytes. The message code the...
13
by: Richard | last post by:
vector<char*> m_Text; m_Text.resize(1); char* foo = "FOO"; char* bar = "BAR"; char* foobar = (char*)malloc(strlen(foo) + strlen(bar) + 1); if (foobar) { strcpy(foobar, foo); strcat(foobar,...
8
by: Marco Costa | last post by:
Hello all, I wrote a simple ODBC wrapper class that used code like this ( not real code, added types for clarification ): char** type bufs = new char* for( int i = 0 ; i < numberOfColumns ;...
4
by: Jim Langston | last post by:
I'm using a function like this: char TextBuffer; jGet_DropDown_Selected_Text( cc.ddSex, TextBuffer); Where the function is filling in the text buffer. I don't have access to the actual...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.