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. Read binary Data and write it back
Forum Update on Monday, May 27th 2025

Read binary Data and write it back

Scheduled Pinned Locked Moved Solved General and Desktop
4 Posts 3 Posters 9.3k 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.
  • F Offline
    F Offline
    Fuel
    wrote on 6 Dec 2017, 05:11 last edited by
    #1

    Good Morning,

    at the Moment i have a Problem and i dont understand it. My Goal is to read a File, like a PDF File, save it to a QByteArray and later write it back to a PDF File. I dont understand how that works. Normally all Files are binary Data that i can read and write back.

    For now i try to open a PDF File that is 995kb big. But the new File i write is only 1kb big. I wrote some Code for safety, but maybe its wrong. Because i get this Output.

    File is bigger then QByteArray
    Filesize in QByteArray doesnt match with Filesize
    read false
    write false
    

    This is my main.cpp

    #include "mainwindow.h"
    #include <QApplication>
    #include <QString>
    #include <QDebug>
    #include "file.h"
    
    int main(int argc, char *argv[])
    {
        QApplication a(argc, argv);
        MainWindow w;
        w.show();
    
        File file;
        bool read, write;
        QString readfile = "C:\\Users\\benni\\Documents\\DMS\\bahn.pdf";
        file.readRawData(readfile, &read);
        QString writefile = "C:\\Users\\benni\\Documents\\DMS\\bahn2.pdf";
        file.writeRawData(writefile, &write);
    
        if (read)
            qDebug() << "read true";
        else
            qDebug() << "read false";
    
        if (write)
            qDebug() << "write true";
        else
            qDebug() << "write false";
    
        return a.exec();
    }
    

    And this are my own File Class

    #include <QFile>
    #include <QDataStream>
    #include <QDebug>
    #include "file.h"
    
    File::File()
    {
    }
    
    File::~File()
    {
    }
    
    void File::readRawData(const QString &filepath, bool *ok)
    {
        QFile file(filepath);
        int filesize = 0;
    
        if (!file.open(QIODevice::ReadOnly))
        {
            qDebug() << "Cant open File: " << filepath;
            *ok = false;
            return;
        }
    
        if (file.size() <= QByteArray::size())
        {
            int size = 4;
            char *tmp = new char[size];
            QDataStream in(&file);
            filesize = in.readRawData(tmp, size);
            mRawData.append(tmp, size);
            file.close();
            delete[] tmp;
            *ok = true;
        }
        else
        {
            qDebug() << "File is bigger then QByteArray";
            *ok = false;
        }
        if (filesize == file.size())
            *ok = true;
        else
        {
            qDebug() << "Filesize in QByteArray doesnt match with Filesize";
            *ok = false;
        }
    }
    
    void File::writeRawData(const QString &filepath, bool *ok)
    {
        QFile file(filepath);
    
        if (!file.open(QIODevice::WriteOnly))
        {
            *ok = false;
            return;
        }
    
        int size = 4;
        QDataStream out(&file);
        out.writeRawData(mRawData.constData(), size);
    
        if (file.size() == mRawData.size())
            *ok = true;
        else
            *ok = false;
    
        file.close();
    }
    

    I hope someone can watch my Code and help me to understand the Steps i need to make.

    D J 2 Replies Last reply 6 Dec 2017, 05:37
    0
    • F Fuel
      6 Dec 2017, 05:11

      Good Morning,

      at the Moment i have a Problem and i dont understand it. My Goal is to read a File, like a PDF File, save it to a QByteArray and later write it back to a PDF File. I dont understand how that works. Normally all Files are binary Data that i can read and write back.

      For now i try to open a PDF File that is 995kb big. But the new File i write is only 1kb big. I wrote some Code for safety, but maybe its wrong. Because i get this Output.

      File is bigger then QByteArray
      Filesize in QByteArray doesnt match with Filesize
      read false
      write false
      

      This is my main.cpp

      #include "mainwindow.h"
      #include <QApplication>
      #include <QString>
      #include <QDebug>
      #include "file.h"
      
      int main(int argc, char *argv[])
      {
          QApplication a(argc, argv);
          MainWindow w;
          w.show();
      
          File file;
          bool read, write;
          QString readfile = "C:\\Users\\benni\\Documents\\DMS\\bahn.pdf";
          file.readRawData(readfile, &read);
          QString writefile = "C:\\Users\\benni\\Documents\\DMS\\bahn2.pdf";
          file.writeRawData(writefile, &write);
      
          if (read)
              qDebug() << "read true";
          else
              qDebug() << "read false";
      
          if (write)
              qDebug() << "write true";
          else
              qDebug() << "write false";
      
          return a.exec();
      }
      

      And this are my own File Class

      #include <QFile>
      #include <QDataStream>
      #include <QDebug>
      #include "file.h"
      
      File::File()
      {
      }
      
      File::~File()
      {
      }
      
      void File::readRawData(const QString &filepath, bool *ok)
      {
          QFile file(filepath);
          int filesize = 0;
      
          if (!file.open(QIODevice::ReadOnly))
          {
              qDebug() << "Cant open File: " << filepath;
              *ok = false;
              return;
          }
      
          if (file.size() <= QByteArray::size())
          {
              int size = 4;
              char *tmp = new char[size];
              QDataStream in(&file);
              filesize = in.readRawData(tmp, size);
              mRawData.append(tmp, size);
              file.close();
              delete[] tmp;
              *ok = true;
          }
          else
          {
              qDebug() << "File is bigger then QByteArray";
              *ok = false;
          }
          if (filesize == file.size())
              *ok = true;
          else
          {
              qDebug() << "Filesize in QByteArray doesnt match with Filesize";
              *ok = false;
          }
      }
      
      void File::writeRawData(const QString &filepath, bool *ok)
      {
          QFile file(filepath);
      
          if (!file.open(QIODevice::WriteOnly))
          {
              *ok = false;
              return;
          }
      
          int size = 4;
          QDataStream out(&file);
          out.writeRawData(mRawData.constData(), size);
      
          if (file.size() == mRawData.size())
              *ok = true;
          else
              *ok = false;
      
          file.close();
      }
      

      I hope someone can watch my Code and help me to understand the Steps i need to make.

      D Offline
      D Offline
      dream_captain
      wrote on 6 Dec 2017, 05:37 last edited by dream_captain 12 Jun 2017, 05:37
      #2

      @Fuel
      It's easy to do with QFile::readAll() .

          //read from file
          QFile inFile("D:/Downloads/mypdf.pdf");
          if (!inFile.exists()) {
              qDebug() << "file doesn't exist";
              return -1;
          }
      
          if (!inFile.open(QIODevice::ReadOnly)) {
              qDebug() << "can't open inFile";
              return -1;
          }
      
          // ready all data
          const QByteArray in = inFile.readAll();
      
          inFile.close();
      
      
          // write into file
          QFile outFile("D:/mypdfcopy.pdf");
          if (!outFile.open(QIODevice::WriteOnly)) {
              qDebug() << "can't open outFile";
              return -1;
          }
      
          outFile.write(in);
          outFile.close();
      
      1 Reply Last reply
      4
      • F Fuel
        6 Dec 2017, 05:11

        Good Morning,

        at the Moment i have a Problem and i dont understand it. My Goal is to read a File, like a PDF File, save it to a QByteArray and later write it back to a PDF File. I dont understand how that works. Normally all Files are binary Data that i can read and write back.

        For now i try to open a PDF File that is 995kb big. But the new File i write is only 1kb big. I wrote some Code for safety, but maybe its wrong. Because i get this Output.

        File is bigger then QByteArray
        Filesize in QByteArray doesnt match with Filesize
        read false
        write false
        

        This is my main.cpp

        #include "mainwindow.h"
        #include <QApplication>
        #include <QString>
        #include <QDebug>
        #include "file.h"
        
        int main(int argc, char *argv[])
        {
            QApplication a(argc, argv);
            MainWindow w;
            w.show();
        
            File file;
            bool read, write;
            QString readfile = "C:\\Users\\benni\\Documents\\DMS\\bahn.pdf";
            file.readRawData(readfile, &read);
            QString writefile = "C:\\Users\\benni\\Documents\\DMS\\bahn2.pdf";
            file.writeRawData(writefile, &write);
        
            if (read)
                qDebug() << "read true";
            else
                qDebug() << "read false";
        
            if (write)
                qDebug() << "write true";
            else
                qDebug() << "write false";
        
            return a.exec();
        }
        

        And this are my own File Class

        #include <QFile>
        #include <QDataStream>
        #include <QDebug>
        #include "file.h"
        
        File::File()
        {
        }
        
        File::~File()
        {
        }
        
        void File::readRawData(const QString &filepath, bool *ok)
        {
            QFile file(filepath);
            int filesize = 0;
        
            if (!file.open(QIODevice::ReadOnly))
            {
                qDebug() << "Cant open File: " << filepath;
                *ok = false;
                return;
            }
        
            if (file.size() <= QByteArray::size())
            {
                int size = 4;
                char *tmp = new char[size];
                QDataStream in(&file);
                filesize = in.readRawData(tmp, size);
                mRawData.append(tmp, size);
                file.close();
                delete[] tmp;
                *ok = true;
            }
            else
            {
                qDebug() << "File is bigger then QByteArray";
                *ok = false;
            }
            if (filesize == file.size())
                *ok = true;
            else
            {
                qDebug() << "Filesize in QByteArray doesnt match with Filesize";
                *ok = false;
            }
        }
        
        void File::writeRawData(const QString &filepath, bool *ok)
        {
            QFile file(filepath);
        
            if (!file.open(QIODevice::WriteOnly))
            {
                *ok = false;
                return;
            }
        
            int size = 4;
            QDataStream out(&file);
            out.writeRawData(mRawData.constData(), size);
        
            if (file.size() == mRawData.size())
                *ok = true;
            else
                *ok = false;
        
            file.close();
        }
        

        I hope someone can watch my Code and help me to understand the Steps i need to make.

        J Offline
        J Offline
        jsulm
        Lifetime Qt Champion
        wrote on 6 Dec 2017, 05:41 last edited by
        #3

        @Fuel

        if (file.size() <= QByteArray::size())
        

        this line does not make any sense (it actually shouldn't compile). size() isn't static. And why do you want to compare byte array size with file size? And which byte array size do you actually mean, from which byte array? QByteArray will adjust its size when needed.
        You are greatly overcomplicating things. If you just want to read the whole file then it is as simple as:

        QFile file(PATH_TO_FILE);
        if (!file.open(QIODevice::ReadOnly))
            return;
        QByteArray data = file.readAll();
        

        https://forum.qt.io/topic/113070/qt-code-of-conduct

        1 Reply Last reply
        5
        • F Offline
          F Offline
          Fuel
          wrote on 6 Dec 2017, 05:48 last edited by
          #4

          Why complicated when its so easy. Thanks to all.

          1 Reply Last reply
          2

          1/4

          6 Dec 2017, 05:11

          • Login

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