#include#include int main() { QFile file("example.txt"); if (!file.open(QIODevice::ReadOnly)) { qWarning() << "Failed to open file"; return 1; } QByteArray data = file.readAll(); qDebug() << "File contents:" << data; file.close(); return 0; }
#includeThis code opens a binary file named "example.bin" in read-only mode. Unlike in the previous example, the read() function is used to read only the first 4 bytes of the file into a QByteArray. The toHex() function is then used to print the hexadecimal representation of the data to the console. The QFile read function is a part of the Qt Core library.#include int main() { QFile file("example.bin"); if (!file.open(QIODevice::ReadOnly)) { qWarning() << "Failed to open file"; return 1; } QByteArray data = file.read(4); qDebug() << "File contents:" << data.toHex(); file.close(); return 0; }