Read binary data.
Unsolved
General and Desktop
-
Hello.
First of all thanks a lot for reading this post and being able to help.I write a binary file like this:
myFile.write(reinterpret_cast<const char *>(&width), sizeof(uint32_t)); myFile.write(reinterpret_cast<const char *>(&height), sizeof(uint32_t)); myFile.write(reinterpret_cast<const char *>(&resx), sizeof(float)); myFile.write(reinterpret_cast<const char *>(&resy), sizeof(float)); myFile.write(reinterpret_cast<const char *>(&origx), sizeof(float)); myFile.write(reinterpret_cast<const char *>(&origy), sizeof(float)); ` Now I would like to read that binary file and get "width and height" in a int. How can I do that? just read the 2 first bytes? Thanks a lot!
-
@AlvaroS
whats the purpose of the binary file?
If you want to read it again using Qt, then simply use QDataStream which handles the correct sizes (reading and writing) for you. -
@VRonin said in Read binary data.:
There is no justification EVER to use reinterpret_cast. it's just bad programming
There is, in some fringe cases, but I relate to the sentiment. ;)
-
for whoever comes here, the correct way is:
Using Qt (myFile is QFile*)
// Write QDataStream writeStream(myFile); writeStream << width << height << resx<< resy<< origx<< origy; // Read QDataStream readStream(myFile); readStream>> width >> height >> resx>> resy>> origx>> origy;