I would like to ask what is the easiest and faster way to convert a file into a stream file.
I did the following :
//convert to stream:
std::string fil= "/home/file.pdf";
std::ifstream t(fil.c_str());
if (t)
{
string res;
string line;
while (getline(t, line, '\n'))
{
res=res+line;
}
std::string p;
p=(base64_encode((reinterpret_cast<const unsigned char *> (res.c_str())),res.size()));
std::string data=p;
char *token = strtok( const_cast<char*>(fil.c_str() ), "/" );
std::string name;
std::vector<int> values;
while ( token != NULL )
{
name=token;
token = strtok( NULL, "/" );
}
std::string f_name=name;
}
//convert from stream to file
ofstream myfile;
std::string fil;
ofstream file (fil.c_str(), ios::out | ios::binary);
std::string content = base64_decode(f_data);
file.write ((char*)&content, sizeof(content));
file.close();
Is this the easiest way?! Is there a posibility to upgrade my code?
EDIT
Code works for .cpp or .txt files. It doesn't work for .pdf files. why?
As far as I know, the easiest way to read a file into a string (byte for byte) using only C++03 standard library facilities is:
Then, you can proceed to whatever processing you want to apply:
If you want to apply base 64 encoding, I'll assume the following signature from your code, and a convenient overload:
Which you can invoke as such: