By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,442 Members | 1,437 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,442 IT Pros & Developers. It's quick & easy.

Reading binary files in C++.

P: n/a

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
Share this Question
Share on Google+
2 Replies


P: n/a
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

P: n/a

"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 discussion thread is closed

Replies have been disabled for this discussion.