C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

ifstream binary read into unsigned char b[LENGTH]

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++)
View previous topic :: View next topic  
Author Message
Steven T. Hatton
Guest





PostPosted: Mon Feb 20, 2006 10:06 am    Post subject: ifstream binary read into unsigned char b[LENGTH] Reply with quote



What is the best way to read data from a file into a fixed size array of
unsigned char? This is a file holding only a SHA1 digest. What I would
really like to do is initialize the ifstream with the buffer allocated to
hold the data, seekg(ios_base::end) and magically have the data appear in
the buffer. I believe something close to this can be done.

I might allocate the array:

const unsigned LENGTH = 256;
unsigned char b[LENGTH];

Then cast it to a std::streambuf

std::streambuf sb(b[LENGTH]);

But can I construct std::ifstream that uses that same memory to hold the
data from its file? How to I prevent reading past the end of my buffer, or
the end of the file data?
--
NOUN:1. Money or property bequeathed to another by will. 2. Something handed
down from an ancestor or a predecessor or from the past: a legacy of
religious freedom. ETYMOLOGY: MidE legacie, office of a deputy, from OF,
from ML legatia, from L legare, to depute, bequeath. www.bartleby.com/61/
Back to top
Rolf Magnus
Guest





PostPosted: Mon Feb 20, 2006 1:06 pm    Post subject: Re: ifstream binary read into unsigned char b[LENGTH] Reply with quote



Steven T. Hatton wrote:

Quote:
What is the best way to read data from a file into a fixed size array of
unsigned char? This is a file holding only a SHA1 digest. What I would
really like to do is initialize the ifstream with the buffer allocated to
hold the data, seekg(ios_base::end) and magically have the data appear in
the buffer.

Why should a seek read any data?

Quote:
I believe something close to this can be done.

I might allocate the array:

const unsigned LENGTH = 256;
unsigned char b[LENGTH];

Then cast it to a std::streambuf

std::streambuf sb(b[LENGTH]);

I don't think that you can be sure that the buffer will contain exactly the
data you want. After all, it's a buffer that's meant for internal use.

Quote:
But can I construct std::ifstream that uses that same memory to hold the
data from its file?

The purpose of streams is text formatting. If you want to read binary data,
just use a filebuf directly.

Quote:
How to I prevent reading past the end of my buffer, or the end of the file
data?

Your idea seems overly complicated to me. Just use the filebuf::sgetn()
function to read as many bytes as you want and you're done.
Back to top
Steven T. Hatton
Guest





PostPosted: Wed Mar 08, 2006 12:06 am    Post subject: Re: ifstream binary read into unsigned char b[LENGTH] Reply with quote



Rolf Magnus wrote:

Quote:
Steven T. Hatton wrote:

What is the best way to read data from a file into a fixed size array of
unsigned char? This is a file holding only a SHA1 digest. What I would
really like to do is initialize the ifstream with the buffer allocated to
hold the data, seekg(ios_base::end) and magically have the data appear in
the buffer.

Why should a seek read any data?

As I understand things, that will (usually?) force the file to be completely
read into memory.

Quote:
Your idea seems overly complicated to me. Just use the filebuf::sgetn()
function to read as many bytes as you want and you're done.

Will that copy the bytes from one location in memory to another? That's
what I want to avoid. This is basically the same thing as move semantics.
Rather than copying the contents of the buffer, I simply want to take
ownership of it.
--
NOUN:1. Money or property bequeathed to another by will. 2. Something handed
down from an ancestor or a predecessor or from the past: a legacy of
religious freedom. ETYMOLOGY: MidE legacie, office of a deputy, from OF,
from ML legatia, from L legare, to depute, bequeath. www.bartleby.com/61/
Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.