Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How to read n bytes of a file?(QT C++)
Forum Update on Monday, May 27th 2025

How to read n bytes of a file?(QT C++)

Scheduled Pinned Locked Moved Solved General and Desktop
6 Posts 2 Posters 631 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    amin_nekounaam
    wrote on last edited by
    #1

    Hi im new to Qt and im trying to read for example the first 4 bytes of my .txt file and show it. I've been searching and figure that QbyteArray may help me best in this situation. so I really like to know how can i read the first 4 bytes of my file with QbyteArray? (appreciate if u write any example code)

    1 Reply Last reply
    0
    • Christian EhrlicherC Offline
      Christian EhrlicherC Offline
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Open the file and then call QFile::read() with the number of bytes you want to read.

      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
      Visit the Qt Academy at https://academy.qt.io/catalog

      1 Reply Last reply
      1
      • A Offline
        A Offline
        amin_nekounaam
        wrote on last edited by
        #3

        actually im try and i think i did it wrong. could you edit the correct code?
        my code:

        QString File_Name = QFileDialog::getOpenFileName(this, "open the file name", QDir::homePath());
        QByteArray data;
        QFile File2(File_Name);
        File2.open(QIODevice::ReadOnly | QFile::Text);
        

        I want to read 4 bytes of File2.

        Christian EhrlicherC 1 Reply Last reply
        0
        • A amin_nekounaam

          actually im try and i think i did it wrong. could you edit the correct code?
          my code:

          QString File_Name = QFileDialog::getOpenFileName(this, "open the file name", QDir::homePath());
          QByteArray data;
          QFile File2(File_Name);
          File2.open(QIODevice::ReadOnly | QFile::Text);
          

          I want to read 4 bytes of File2.

          Christian EhrlicherC Offline
          Christian EhrlicherC Offline
          Christian Ehrlicher
          Lifetime Qt Champion
          wrote on last edited by
          #4

          @amin_nekounaam said in How to read n bytes of a file?(QT C++):

          I want to read 4 bytes of File2.

          And what's the problem - as I said above use QFile::read() for this task.
          And you should check the return value of QFile::open()

          Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
          Visit the Qt Academy at https://academy.qt.io/catalog

          A 1 Reply Last reply
          0
          • Christian EhrlicherC Christian Ehrlicher

            @amin_nekounaam said in How to read n bytes of a file?(QT C++):

            I want to read 4 bytes of File2.

            And what's the problem - as I said above use QFile::read() for this task.
            And you should check the return value of QFile::open()

            A Offline
            A Offline
            amin_nekounaam
            wrote on last edited by
            #5

            @Christian-Ehrlicher said in How to read n bytes of a file?(QT C++):

            Thanks got it. Solved.

            Christian EhrlicherC 1 Reply Last reply
            0
            • A amin_nekounaam

              @Christian-Ehrlicher said in How to read n bytes of a file?(QT C++):

              Thanks got it. Solved.

              Christian EhrlicherC Offline
              Christian EhrlicherC Offline
              Christian Ehrlicher
              Lifetime Qt Champion
              wrote on last edited by
              #6

              @amin_nekounaam Then also mark this topic as solved, thx.

              Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
              Visit the Qt Academy at https://academy.qt.io/catalog

              1 Reply Last reply
              0

              • Login

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • Users
              • Groups
              • Search
              • Get Qt Extensions
              • Unsolved