Skip to main content
Rick E Johnson
Inspiring
March 3, 2023
Answered

store binary file in document dictionary, then recreate the file

  • March 3, 2023
  • 1 reply
  • 466 views

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);

This topic has been closed for replies.
Correct answer Rick E Johnson

I found the probem. Getting the file size of the input file set the file marker at the end, and I just needed to move it back to the beginning of the file before reading it in.

 

infile.seekg(0, std::ios::beg); // set marker back to beginning of the file

1 reply

Rick E Johnson
Rick E JohnsonAuthorCorrect answer
Inspiring
March 9, 2023

I found the probem. Getting the file size of the input file set the file marker at the end, and I just needed to move it back to the beginning of the file before reading it in.

 

infile.seekg(0, std::ios::beg); // set marker back to beginning of the file