Hi,
we are trying to use file_images in our app to reduce time and optimize some part of code.
However we succefully create file_image from existing on disc file. next we make modification of attributes and other data.
On end we want to save this data stored in file_image to disc and this doesn`t work.
Our purpose is to create file in memory (file_image), then when user is doing actions in our app, this data is modified many times.
However when user finish to work with loaded file we want to save it back into the file on disc.
Below i give example code we use.
There must be something we do wrong. Or maybe this can`t be done this way ?
Thanks for any ideas and answers.
int main()
{
std::wstring filename("some path to file on disc");
//// reading file to buffer with standard winapi way
HANDLE hFile = CreateFile(filename.c_str(), GENERIC_READ | GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL,0);
if (hFile == INVALID_HANDLE_VALUE)
return -1;
int buffer_size = GetFileSize(hFile, NULL);
char* buffer = new char[buffer_size];
DWORD bytesReaded = 0;
ReadFile(hFile, buffer, buffer_size, &bytesReaded, 0);
CloseHandle(hFile);
//// creating image from given readed buffer
unsigned flags = H5LT_FILE_IMAGE_DONT_COPY | H5LT_FILE_IMAGE_DONT_RELEASE | H5LT_FILE_IMAGE_OPEN_RW;
hid_t fileid = H5LTopen_file_image(buffer, buffer_size, flags);
//// bind image with file to modify attributes
H5::H5File h5file;
h5file.setId(fileid);
{
H5::Group root = h5file.openGroup("/");
H5::Attribute a = root.openAttribute("SomeImportantAttrib");
size_t size = a.getStorageSize();
H5::StrType dtString(H5::PredType::C_S1, size);
dtString.setCset(H5T_CSET_UTF8);
std::string value(size, '\0');
a.read(dtString, value); // on start this produce value = "val1"
std::string valueR = "val2";
dtString.setSize(valueR.length());
a.write(dtString, valueR.c_str()); //this goes ok
size_t size2 = a.getStorageSize();
dtString.setSize(size2);
std::string value2(size2, '\0');
a.read(dtString, value2); // this produce value2 = "val2" - so it is ok
H5::Attribute a2 = root.createAttribute("testAttr", dtString, H5::DataSpace());
a2.write(dtString, "newAttrInGroup");
root.close();
}
//// getting buffer and size after modifications
int buffer_size2 = H5Fget_file_image(h5file.getId(), 0, 0);
char* buffer2 = new char[buffer_size2];
int result2 = H5Fget_file_image(fileid, buffer2, buffer_size2);
//// writing buffer to disc with standard winapi way
std::string filename2("some other path to file on disc");
std::vector< wchar_t > wfilename(strlen(filename2.c_str()) + 1);
MultiByteToWideChar(CP_UTF8, 0, filename2.c_str(), -1, &wfilename[0], wfilename.size());
HANDLE hFile2 = CreateFile(&wfilename[0], GENERIC_WRITE, FILE_SHARE_READ, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, 0);
if (hFile2 == INVALID_HANDLE_VALUE)
return -1;
DWORD bytesWrited = 0;
WriteFile(hFile2, buffer2, buffer_size2, &bytesWrited, 0);
CloseHandle(hFile2);
return 0;
}