Solved How can I read strings from a binary file?
-
This is what I have so far:
QFile file; QString str1, str2; file.setFileName(file_name); if (file.open(QIODevice::ReadOnly)) { QDataStream data(&file); data >> str1; data >> str2; } file.close();
Will this work or do I have to store the string sizes in the file?
-
Hi,
What do you use to write that file ?
-
@Guerrian Assuming you wrote it with a QDataStream/QFile as binary then no you will not need the lengths of each string.
Edit: as a side note you could have just compiled your example and tested it faster than writing this question. :D
-
@ambershark Not really, because I would have had two versions of my program each with a load and a save function... too much editing.
-
@Guerrian Yea but I literally took your code, added it to a
main()
, wrote a quick file using QFile as binary, and then read it using your code. Took me < 2 minutes. You didn't have to edit your actual code, you can just create a test app. Even if you were super slow and it took you 20 min to test it, it's still faster than asking a forum. ;) -
This post is deleted! -
@ambershark I sussed this out, but it took me a lot longer than 20 minutes. I'm new to Qt and my program had a bug which I had to fix before getting this right. It works like you said. Thanks.
-
@Guerrian Yea sorry I didn't factor learning time or bugs into my times. :) Unfortunately while learning everything takes so much longer. But the experience is what makes the memory in the future.