1

I need to read a file as a binary code with Qt tools. In std c++ I did it like that:

#include <iostream>
#include <vector>
#include <fstream>

int main() {
    std::ifstream file("C:\\Users\\%username%\\Desktop\\programme.exe", 
    std::ios::binary);
    std::vector<char> vec((std::istreambuf_iterator<char>(file)),
        (std::istreambuf_iterator<char>())); // that variable has the binary code
    file.close();
    return 0;
}
eyllanesc
  • 235,170
  • 19
  • 170
  • 241
JoeWest13
  • 11
  • 1

1 Answers1

2

Qt is written using standard c++ as well as using native calls depending on OS and architecture. Therefore normal c++file I/O still works and is portable, otherwise have a look at the QFile documentation here. You would want to have a look at the examples that mention QDataStream

Werner Erasmus
  • 3,988
  • 17
  • 31
  • This is the answer; however, just to nitpick, Qt(at least version 5) is not standard conforming in general. The reason that the use of Qt doesn't matter here is that Qt is a just a library, meaning that all the usual C++ stuff still works. – rationalcoder Oct 13 '17 at 21:03
  • @rationalcoder, how is Qt not standard conforming? It's compiled with a stock standard compiler. Granted, it uses it's own DSL, but so does some boost libs – Werner Erasmus Oct 13 '17 at 21:13
  • @WernerErasmus It is only non-conforming in a few small places. See https://stackoverflow.com/questions/36893251/why-does-the-enhanced-gcc-6-optimizer-break-practical-c-code. If you consult the GCC changelog, you will find more of these cases if I remember correctly. – rationalcoder Oct 16 '17 at 18:49
  • @rationalcoder, see http://lists.qt-project.org/pipermail/development/2016-March/025166.html. Perhaps (the SO link you reference is) dated in terms of qt after all. You have to realise that they also make an effort to comply, therefore rather show me the non conforming code that applies presently – Werner Erasmus Oct 17 '17 at 05:40
  • @WernerErasmus. Their effort isn't the point. The point is that in a framework as large as Qt and with a language as complicated as C++, there will pretty much always be compiler specific and exception safety weirdness along with UB in deprecated code (especially surrounding threading and signals and slots) at any given point in time. The merit of your answer is not in question, I am just pointing out that there has always been at least _some_ non-conforming weirdness present in every version of Qt that I have used. – rationalcoder Oct 18 '17 at 18:17