473,385 Members | 1,838 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,385 software developers and data experts.

ifstream binary read into unsigned char b[LENGTH]

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/
Feb 20 '06 #1
2 6968
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?
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.
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.
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.

Feb 20 '06 #2
Rolf Magnus wrote:
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.
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/
Mar 8 '06 #3

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

Similar topics

5
by: Nils Wogatzky | last post by:
Hi, I´ve got a problem with the iftream.read method. I´m reading out a binary file, but I receive wrong values if values are negative. m_File.read((char*)&help2,2); so, help2 is...
1
by: wtnt | last post by:
Hello. I've searched all over and haven't seen another thread with this problem. Please bear with me as I try to explain. thanks. :) I have some programs that need to be cross-platform...
6
by: Ram Laxman | last post by:
Iam new bie to C++ programming.. I want to write a program which will read the Comma separated values(CSV) file column wise. For example: In a.txt: "TicketNumber","Phone","CarNumber"...
12
by: Steven T. Hatton | last post by:
I know of a least one person who believes std::ifstream::read() and std::ofstream::write() are "mistakes". They seem to do the job I want done. What's wrong with them. This is the code I...
28
by: wwj | last post by:
void main() { char* p="Hello"; printf("%s",p); *p='w'; printf("%s",p); }
2
by: Karl | last post by:
Hey everyone! I've got a quick question on whether std::ifstream is buffered or not. The reason is that I have a homework assignment that requires me to benchmark copying files using different...
9
by: Notebooker | last post by:
Hello, I'm an intermediate noob reading-in data from ascii-file using an ifstream object. I have specified a c-style string buffer with size of type size_t and I am specifying to use this...
4
by: uche | last post by:
Hello, I am having a problem with my code in C++/C. I have struggled for a while and cannot find the solution to the problem... Here is the compiled..output: In file included from disk.cpp:8:...
4
by: supemoy | last post by:
I am having a heck of a time using the ifstream::read() method. I am using Visual Studio 6.0, and my intelisense indicates that read has an overload of: ifstream::read(unsigned char*, int _n) A...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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,...

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.