Read binary Data and write it back



  • 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.



  • @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();
    

  • Moderators

    @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();
    


  • Why complicated when its so easy. Thanks to all.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.