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++.

Thanks
prain
prainAsked:
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.

sarabandeCommented:
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 (!file.read((char*)&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]-

Sara
0
jkrCommented:
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;

stat(pFile,&buf);

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

ifstream is(pFile);

is.read((char*)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.
0

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.

prain
0
Cloud Class® Course: Amazon Web Services - Basic

Are you thinking about creating an Amazon Web Services account for your business? Not sure where to start? In this course you’ll get an overview of the history of AWS and take a tour of their user interface.

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

Open in new window

Sorry about that.
0
prainAuthor Commented:
More points.
0
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.
-prain
0
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.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.