2

I created function Qt to read in a binary file, and it works.

[code]

    if (fileLoad.open(QIODevice::ReadOnly))
   {
   QDataStream in(&fileLoad);

   quint8 Variable_8bits;
   quint16 Variable_16bits;
   quint32 Variable_32bits;

   in >> Variable_16bits >> Variable_8bits >> Variable_32bits >> ZeroByte;

   qDebug() <<  Variable_16bits << Variable_8bits << Variable_32bits;

   //Works no extreme conversion necessary as i read input with "set size variables"
   // first 16bits, then 8bits, then 32bits
   // and store it correctly for display
   }
   fileLoad.close(); 
   }

So basically I could read in a binary file, using variables of different sizes to access the values in the file (Since I know the format of file structure)

My issue is that, now I need to create the same or similar functionality to a standard c++ function.

Is there a DataStream like Qt for C++ Or do I have to manually load file into buffer, then read in individual bytes, do bitwise manipulations to get the correct representation length, before i display the value or if there is a simpler method...

whats the way forward...

László Papp
  • 51,870
  • 39
  • 111
  • 135
2lights
  • 41
  • 2
  • What exactly is the issue with the Qt version? You cannot use it on some platform, or what is the problem with Qt in general? Also, do you need to support pre-c++11 toolchains? Have you looked into std::ifstream? – László Papp Jan 10 '14 at 10:00
  • 1
    @2lights Note that `QDataStream` is not for reading and writing just any binary files. It uses its own serialization format, and is meant for reading and writing that. The format is simple, so for *some* types the serialization format is same as just writing raw bytes from memory to file directly, but then the benefit of using `QDataStream` for that purpose is dubious. – hyde Jan 10 '14 at 10:09
  • @hyde: x86 fixed mind – Marek R Jan 10 '14 at 10:53
  • 1
    to be more precise in this case on x86 machine omitting `QDataStream` will give different result, since by default `QDataStream` uses big endian and x86 are using small endian. – Marek R Jan 10 '14 at 11:00

2 Answers2

2

In standard C++ there is no function/class with functionality similar to QDataStream. Pleas note that QDataStream class provides support for multiple architectures, it take into account endians (by default assumes big endian), different standards of floating point values, it take control of sizes of build in types. (it also supports internalization and externalization of some Qt classes but this issue does not applies to standard C++)
In standard C++ all this platform diversity has to be handled manually (or by library).

Marek R
  • 32,568
  • 6
  • 55
  • 140
  • please comment when downvoting I wish to knew why I am wrong (or not) – Marek R Jan 10 '14 at 10:46
  • I will give you _my_ personal opinion: the OP's issue does not seem to have anything to do with float, "internalization" (for ints?), or "externalization, etc, IMHO. – László Papp Jan 11 '14 at 08:14
0

I would use std::ifstream for this simple use case presented as follows below.

However, note that the 8, 16, and 32 bit types were only added to the standard in C++11 and on.

#include <iostream>
#include <fstream>
#include <string>

using namespace std;

int main ()
{
    string line;
    ifstream myfile("example.txt", std::ios::binary);
    uint8_t Variable_8bits; // C++11 and on
    uint16_t Variable_16bits; // C++11 and on
    uint32_t Variable_32bits;  // C++11 and on
    if (myfile.is_open())
    {
        myfile.read(&Variable_16bits, 2);
        myfile.read(&Variable_8bits, 1);
        myfile.read(&Variable_32bits, 4)
        ...

        std::out << Variable_16bits << Variable_8bits << Variable_32bits;
        myfile.close();
    } else {
        cout << "Unable to open file"; 
    }

    return 0;
}
László Papp
  • 51,870
  • 39
  • 111
  • 135
  • 1
    operators: `>>` `<<` in iostream are for TEXT STREAMS ONLY! Question is about binary streams. – Marek R Jan 10 '14 at 10:14
  • @MarekR: obsolete, consider removing your comment. – László Papp Jan 10 '14 at 10:16
  • why did you vote my answer down without comment? Does your solution handles endians, or different standards of floating points? – Marek R Jan 10 '14 at 10:23
  • @MarekR: why do you think I have downvoted your comment? Also, please stop begging for download reason under my answer. :) But there is no need for endianness in this simple scenario, nor "internalization". I do not even know what "externalization" means. As for the float, that is also moot here since the variables used are integer. Also, please remove your obsolete comment if I may ask. – László Papp Jan 10 '14 at 10:26
  • Only we are active in this question, there is no other hint who or more important why downvoted my answer. Commenting under my question will not notify anyone about it. http://www.yourdictionary.com/externalization – Marek R Jan 10 '14 at 10:37
  • Please stop discussing your downvote under my answer. Thank you for your understanding. As for the externalization, it is not even remotely related to QDataStream as it seems. – László Papp Jan 10 '14 at 10:39
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/44941/discussion-between-marek-r-and-laszlo-papp) – Marek R Jan 10 '14 at 10:39
  • I don't see how that comment is obsolete. If I attempted to encode a uint8_t the iostream >> << operators would consider this a character for ASCII interpretation instead of encoding the actual underlying value which is undesirable behavior when dealing with data streams. – Klypto Jun 14 '21 at 19:51