Unsolved Writing raw data to a file using QFile
-
@Smeeth
I'm afraid I'm just the wrong to person to ask that! I'd gomemcpy(char_bur, &status, sizeof(status))
, or write aunion
or use a(unsigned char *)
cast. (I wouldn't get a "implicit conversion changes signedness", you shouldn't be accessing theint
as anint
, only its address. Or more likestatic_cast<char *>&status
.) And make aQByteArray
from thechar
buffer/pointer.(I'd really just go
fwrite(&status, sizeof(status), 1, file_pointer)
, and the whole thing would be done in one line).None of which doubtless is at all allowed/encouraged now. So hopefully a C++ expert will offer some C++ or Qt friendly ways to get those bytes out of the variables.... :)
-
Thank you for your help. I believe I saw someone else suggest using memcopy in another thread somewhere so I will pursue that route. Thanks!
-
@Smeeth
Please don't say I encouraged you to usememcpy
. I'll get thrown out of the forum. -
@JonB I swear I heard it elsewhere! :)
-
const char* charbytes{reinterpret_cast<const char*>(original_data)}; int size{static_cast<int>(sizeof(original_type))}; QByteArray bytes{charbytes, size};
-
Works the other way, too:
auto myStruct = reinterpret_cast<MyStruct*>(bytearray.data()); // "The pointer remains valid as long as the byte array isn't reallocated or destroyed."
The same structure in both ends is of course handy unless you need really variable data. In the latter case you have to create a complicated protocol and it would be better to use a higher level protocol.
-
If you want to write raw data, use raw C functions :)
#include <cstdio> FILE * file; file = fopen ("MyFile.dat", "wb"); fwrite (&status , 1, 1, file); fwrite (&channel , 1, 1, file); fwrite (&sourceVal1 , sizeof(float), 1, file); fwrite (&sourceVal2 , sizeof(float), 1, file); fwrite (&ticks , sizeof(ticks), 1, file); fclose (file);
-
@mpergand
OMG! But that's what I said. I assumed you'd be shot for that here!
To be fair, I assumed the OP would want to use theQFile
class to do his writing. Isn't one of the points of Qt to useQFile
etc. instead of the C/C++ stdio stuff? -
What about QFile::writeData()?
-
To get back to the original question, I think we derailed a bit with QByteArray
@Smeeth said in Writing raw data to a file using QFile:
I have some data (uint8_t, uint32_t, float) that I want to write to a raw data file, without any extra information.
Never the less @aha_1980 is right,´qint64 QIODevice::write(const char *data, qint64 maxSize)´ is the way to go
this, should do just fine, its untested however.
QFile f (...); .... uint8_t var1; uint32_t var2; float var3; f.write(reinterpret_cast<const char *>(&var1), sizeof(uint8_t)); f.write(reinterpret_cast<const char *>(&var2), sizeof(uint32_t )); f.write(reinterpret_cast<const char *>(&var3), sizeof(float ));
-
@J.Hilk
Works fine :)template< class aType > qint64 write( QFile& file, aType var ) { qint64 toWrite = sizeof(decltype (var)); qint64 written = file.write(reinterpret_cast<const char*>(&var), toWrite); if (written != toWrite) { qDebug () << "write error"; } qDebug () << "out: " << written; return written; } template< class aType > qint64 read( QFile& file, aType &var ) { qint64 toRead = sizeof(decltype (var)); qint64 read = file.read(reinterpret_cast<char*>(&var), toRead); if (toRead != read) { qDebug () << "read error"; } qDebug () << "in: " << read; return read; } QFile file("e:/test.txt"); if (!file.open(QFile::WriteOnly)) { return; } uint8_t var1 = 10; uint32_t var2 = 20000; float var3 = 10.8; write(file, var1); write(file, var2); write(file, var3); file.close(); var1 = 0; var2 = 0; var3 = 0; if (!file.open(QFile::ReadOnly)) { return; } read(file, var1); read(file, var2); read(file, var3); qDebug() << " result = " << var1 << " " << var2 << " " << var3;
out: 1
out: 4
out: 4
in: 1
in: 4
in: 4
result = 10 20000 10.8but file is fragile to platform change etc.