473,788 Members | 2,672 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.d at") ;

std::istreambuf _iterator beg(input), end ;

std::vector DataFile(beg,en d) ;

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 4136
me wrote:
Hi guys - the question is in the subject line.

I thought of one quick way:

std::ifstream input("myfile.d at") ;

std::istreambuf _iterator beg(input), end ;

std::vector DataFile(beg,en d) ;

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<cha r> DataFile(filesi ze);
input.read(&Dat aFile[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.d at") ;

std::istreambuf _iterator beg(input), end ;

std::vector DataFile(beg,en d) ;

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<cha r> DataFile(filesi ze);
input.read(&Dat aFile[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.d at") ;

std::istreambuf _iterator beg(input), end ;

std::vector DataFile(beg,en d) ;
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.d at") ;

std::istreambuf _iterator beg(input), end ;

std::vector DataFile(beg,en d) ;

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
1891
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 used and therefore will be less than optimal. Can anyone help me out? Regards, Steven
2
4775
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 second 4. The total message length is therefore 4 + message length. A number of messages work fine/as expected but there are consistant errors occuring. After a period
13
4654
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, bar); }
8
4324
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 ; i++ ) { size = getSizeOfColumn(); bufs = new char;
4
2389
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 function to change it to a std::string (it's a library function) so I was thinking, well, I could use a std::vector<charinstead of a char array, but would that actually gain me anything at all? I mean, what's the difference between calling it like the...
0
9656
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9498
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
10118
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
7519
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5403
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5538
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4074
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3677
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2897
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.