   C++ File IO Problem
  Well I've written a little class to encrypt simple text files and I've come up with a problem. In the source code you can see the array of chars is big enough (5000000) to read a 54KB file and encrypt it, but it encrypts only about 1/18 of the file (3KB). How come? Have I missed something?void CEncrypt::operator()(unsigned short usNumericalSystem, string strFilePath){char* pchFileCont = new char[5000000];string* pstrFileCont = new string;ifstream* is = new ifstream(strFilePath.c_str());ofstream* os = new ofstream(("encrypted_" + strFilePath).c_str());is->read(pchFileCont, 5000000);*pstrFileCont = pchFileCont;this->operator()(usNumericalSystem, pstrFileCont);is->close();os->write(pstrFileCont->c_str(), (streamsize)pstrFileCont->size());os->close();delete[] pchFileCont;delete pstrFileCont;delete is;delete os;}Thanks in advance!   
  Perhaps your encryption method is creating null bytes in the string?   ;  Just a wild guess:  The encryption is producing NULL bytes and "terminating" the string so os->write() doesn't get the correct size to output.Edit: Too slow... :);  Well it turns every letter to a number and saves it. When I decrypt the file with the other class CDecrypt it decrypts only 1/18 of the file. The code for IO in CDecrypt is the same.   ;  More likely it's because he's writing in text mode and probably writing an end-of-file character.  Open your output stream in binary mode instead (pass std::ios::binary as a second parameter).Also, why are you declaring all of your local variables on the heap?  It's inefficient and unsafe.  Prefer to use the stack wherever possible:void CEncrypt::operator()(unsigned short numericalSystem, string filePath){vector< char > fileContents(5000000);ifstream is(filePath.c_str());ofstream os(("encrypted_" + filePath).c_str(), std::ios::binary);is.read(&fileContents[0], 5000000);this->operator()(numericalSystem, fileContents);os.write(&fileContents[0], fileContents.size());}And please read this article about hungarian notation.Σnigma   ;  Well writing in the binary mode didn't change anything. Regarding the notation , I'm used to it.   ;  You also have to open the file in binary mode when you decrypt.  The mode the files are opened in by default are most likely not what you want.  You need to specify the flags.   ;  Having now seen your second post in this thread (which you must have submitted after I started writing my reply) it almost certainly is embedded nulls and it is your decryption code that is at fault, not your encryption code.  Try this correction to my previously posted code (I hadn't realised you were overallocating your buffer before):void CEncrypt::operator()(unsigned short numericalSystem, string filePath){ifstream is(filePath.c_str());vector< char > fileContents((istreambuf_iterator< char >(is)), istreambuf_iterator< char >());ofstream os(("encrypted_" + filePath).c_str(), std::ios::binary);this->operator()(numericalSystem, fileContents);os.write(&fileContents[0], fileContents.size());}istreambuf_iterator is found in header <iterator>.EDIT: You'll need to make similar changes to the decryption function as well.Σnigma   ;  Is there a non-vector solution, cause I have almost everything based on strings. Thanks for efforts anyways.   ;  Is there a non-vector solution, cause I have almost everything based on strings. I wouldn't need to rearrange everything then. Thanks anyways   