store binary file in document dictionary, then recreate the file
I'm trying to read a binary file, store it in the doc dictionary, then read it back from the dictionary and re-create the binary file. It recreates a new file of the same size, but it gets corrupted at some stage -- don't know if it's reading or writing it wrong. Any suggestions would be much appreciated. Here is abbreviated code:
// READ FILE
std::ifstream infile("file1.bin", std::ios::in | std::ios::binary);
infile.seekg(0, std::ios::end);
int size = infile.tellg();
//read in the contents of the file
char * buffer = new char[size];
infile.read(buffer, size);
error = sAIDocument->GetDictionary(&doc_dictionary);
error = sAIArray->CreateArray(&array_ref); // store in new array element
error = sAIArray->AppendEntry(array_ref, sAIEntry->FromBinary(buffer, size));
//release the memory for the buffer
delete[] buffer;
//close the file
infile.close();
sAIDictionary->Release(doc_dictionary);
// WRITE FILE
size_t size = 0;
sAIEntry->AddRef(sAIArray->Get(array_ref, 0));
error = sAIEntry->ToBinary(sAIArray->Get(array_ref, 0), NULL, &size);
char* buffer = (char*) malloc(size);
error = sAIEntry->ToBinary(sAIArray->Get(array_ref, i), buffer, &size);
// write data to binary file
std::ofstream outfile ("file2.bin", std::ios::binary);
if (outfile.is_open()) {
outfile.write(buffer, size);
outfile.close();
}
free(buffer);
