0

What I am trying to do is create an encryption tool. It converts a file to string. That string is then to be encrypted. However, while converting file to string and again saving that file (for testing purposes), the returned file is not the same as the original file and does not open even after I change the extension. Please help:

String^ fileNME = textBox1->Text;   //File is selected via fileopendialog and then copied to TextBox1

    //Convert from System::String to std::string. result is actually a string that holds     the filename.
    string result("a");

    result = marshal_as<std::string>( fileNME );



    string fileToEncryptasString;

    fileToEncryptasString = fileToString(result);

myfile.open ("filecopy.txt");
myfile << fileToEncryptasString;
myfile.close();

//File to String function is this:

string fileToString(const string& filename)
{
ifstream file(filename, ios::binary);
if (!file) return "";
string str(istreambuf_iterator<char>(file),
           (istreambuf_iterator<char>()));
return str;
}
President James K. Polk
  • 36,717
  • 16
  • 86
  • 116
  • Are you sure there aren't `fileToString` functions available elsewhere? Why do you try to create this yourself? – Maarten Bodewes Sep 07 '14 at 10:57
  • I did not create this myself but borrowed it from somewhere. Is this function the problem? – Adeel Malik Sep 07 '14 at 11:05
  • Probably. Read [this](http://stackoverflow.com/a/2602060/589259), especially the edit. – Maarten Bodewes Sep 07 '14 at 11:38
  • I'd prefer the [first answer of Jerry](http://stackoverflow.com/a/2602258/589259) to the one you're using. Just removing some parenthesis should not significantly alter the code. I can just see my colleagues removing them because they seem redundant. – Maarten Bodewes Sep 07 '14 at 11:48
  • I have checked Jerry's and Tyler's functions. They eat up a file and return only a few bytes. For example of a 1 MB file only 1.2 KB salt.txt is formed. Whenever I return a text file (.txt) The return for .txt file is perfect, however .jpg or .pdf file returns corrupted. Why is that? Also my function adds extra bytes for reasons unknown. – Adeel Malik Sep 07 '14 at 12:27
  • Are you sure you aren't handling binary data as character strings? If you do a `strlen` on binary data it will count bytes until it finds an EOF (0x00) value. – Maarten Bodewes Sep 07 '14 at 12:54

0 Answers0