How to convert a Text File into a byte array in C++

Any one knows how to convert a given text file (unknown size) into a byte array in C++. Please do not answer this in C or C# or Java. I want this in C++.

Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

you would retrieve the size of the text file using stat function:

#include "sys/stat.h"
std::string filename = "full_path_of_file";
struct stat status;
if (stat(filename.c_str(), &status) != 0)
    // error

Open in new window

then open the file with

std::ifstream file(filename.c_str());

Open in new window

allocate byte array by

std::vector<unsigned char> bytearray(status.st_size);

Open in new window

read the file with

if (!*)&bytearray[0],  bytearray.size())
       // error

Open in new window

close the file. the bytearray could be accessed via the vector or as unsigned char * by &bytearray[0]-

You can do that like
#include <sys/stat.h>

#include <fstream>
using namespace std;

typedef unsigned char BYTE;


const char* pFile = "test.txt";

struct stat buf;


BYTE* pAdd = new BYTE[buf.st_size];

ifstream is(pFile);*)pArr,buf.st_size);

// access each byte via pArr[i];

delete [] pArr; // clean up

Open in new window

If you are on Windows, use '_stat(pFile,&buf);' instead.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
prainAuthor Commented:
Hello jkr,
you meant pAdd and not pArr right? I do not see pAdd is being used anywhere after it is defined.

Exploring SQL Server 2016: Fundamentals

Learn the fundamentals of Microsoft SQL Server, a relational database management system that stores and retrieves data when requested by other software applications.

Actually, it should be
BYTE* pArr = new BYTE[buf.st_size];

Open in new window

Sorry about that.
prainAuthor Commented:
More points.
prainAuthor Commented:
Ok, with all due respect to sarabande's answer, I accept jrk's because it fits our coding style well.

I have increased the points to 200 and given the increased amount to sarabande, while jrk get full points I promised first.

Thanks guys again.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Programming Languages-Other

From novice to tech pro — start learning today.