Reading binary files in C++.

J

Jason Heyes

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;
}
 
K

Karl Heinz Buchegger

Jason said:
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.
 
M

Mike Wahler

Jason Heyes said:
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;
}

-Mike
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top