473,394 Members | 2,071 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,394 software developers and data experts.

Reading binary files in C++.


I tried to use this function to read a binary file. It only reads a few
thousand bytes and not the entire file. How do I change it to read a whole
binary file? Thanks in advance.

bool read_file(const char *fname, std::string &data)
{
std::ifstream in(fname);
if (!in.is_open())
return false;

std::stringstream ss;
if (!(ss << in.rdbuf()))
return false;

data = ss.str();
return true;
}
Oct 25 '05 #1
2 2071
Jason Heyes wrote:

I tried to use this function to read a binary file. It only reads a few
thousand bytes and not the entire file. How do I change it to read a whole
binary file? Thanks in advance.

bool read_file(const char *fname, std::string &data)
{
std::ifstream in(fname);
if (!in.is_open())
return false;

std::stringstream ss;
if (!(ss << in.rdbuf()))
return false;

data = ss.str();
return true;
}


OPen the file in binary mode.

--
Karl Heinz Buchegger
kb******@gascad.at
Oct 25 '05 #2

"Jason Heyes" <ja********@optusnet.com.au> wrote in message
news:43**********************@news.optusnet.com.au ...

I tried to use this function to read a binary file. It only reads a few
thousand bytes and not the entire file. How do I change it to read a whole
binary file? Thanks in advance.

bool read_file(const char *fname, std::string &data)
{
std::ifstream in(fname);
std::ifstream in(fname, std::ios::binary); /* needs #include <ios> */
if (!in.is_open())
return false;

std::stringstream ss;
if (!(ss << in.rdbuf()))
return false;

data = ss.str();
return true;
}


-Mike
Oct 25 '05 #3

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

Similar topics

2
by: christos panagiotou | last post by:
hi all I am trying to open some .raw files that represent images (256x256, 8 bit per pixel, no header) in a c++ program I cannot copy paste the module here as it uses a method from the VTK...
3
by: Olivier Maurice | last post by:
Hi all, I suppose some of you know the program Redmon (type redmon in google, first result). This neat little tool allows to hook up any functionality to a printer by putting the file printed...
7
by: laclac01 | last post by:
So I am converting some matlab code to C++. I am stuck at one part of the code. The matlab code uses fread() to read in to a vector a file. It's a binary file. The vector is made up of floats,...
50
by: Michael Mair | last post by:
Cheerio, I would appreciate opinions on the following: Given the task to read a _complete_ text file into a string: What is the "best" way to do it? Handling the buffer is not the problem...
5
by: rnorthedge | last post by:
I am working on a code library which needs to read in the data from large binary files. The files hold int, double and string data. This is the code for reading in the strings: protected...
7
by: John Dann | last post by:
I'm trying to read some binary data from a file created by another program. I know the binary file format but can't change or control the format. The binary data is organised such that it should...
2
by: amfr | last post by:
On windows, is there anything special I have to do to read a binary file correctly?
30
by: siliconwafer | last post by:
Hi All, I want to know tht how can one Stop reading a file in C (e.g a Hex file)with no 'EOF'?
2
by: nnimod | last post by:
Hi. I'm having trouble reading some unicode files. Basically, I have to parse certain files. Some of those files are being input in Japanese, Chinese etc. The easiest way, I figured, to distinguish...
6
by: arne.muller | last post by:
Hello, I've come across some problems reading strucutres from binary files. Basically I've some strutures typedef struct { int i; double x; int n; double *mz;
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: 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
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
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
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.