Error reading with QDataStream

0

Hi, I'm using qdatastream to read binary data from a stream. Well the code that basically implements is the following:

QFile *archivoStub;
archivoStub = new QFile();
archivoStub->setFileName(QApplication::applicationFilePath());
archivoStub->open(QFile::ReadOnly);
/* Leer los datos de la trama*/
QDataStream in(&archivoStub);
QString trama;
qint32 a;

The problem is that when I read the data it shows me the following error:

  

argument 1 can not be converted from 'QFile **' to 'QIODevice *'   The types indicated are not related; the conversion requires   reinterpret_cast, conversion of C style or conversion of style of   function

My question is this compilation error first why is it due? And second, that my file will contain several executables, a data frame and an integer that defines the size of my data frame. Is there any way to serialize this data so that it is easily readable.

When I say serialize I mean:

  

If with qdatastream I can order several elements and read them. Then   also if I use qdatastream and there is another type contained as files   could read my series or if it is not possible I would have to implement everything   with qdatastream.

The code in case you still do not understand what I mean. Together several files:

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QFile>
#include <QFileDialog>
#include <QTextStream>

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    connect (ui->botonExaminar1,SIGNAL(clicked()),this,SLOT(examinar1()));
    connect (ui->botonExaminar2,SIGNAL(clicked()),this,SLOT(examinar2()));
    connect (ui->botonUnir,SIGNAL(clicked()),this,SLOT(juntar()));
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::examinar1()
{
    ui->ejecutable1Texto->setText(QFileDialog::getOpenFileName(this,"Abrir archivo"));
}
void MainWindow::examinar2()
{
    ui->ejecutable2Texto->setText(QFileDialog::getOpenFileName(this,"Abrir archivo"));
}
void MainWindow::juntar()
{
    /** función para juntar los dos ejecutables **/

    /* declaraciones */
    QFile archivoSalida;
    QFile *archivoEjecutable1;
    QFile *archivoEjecutable2;
    QFile *archivoStub;
    QByteArray tamano1;
    QByteArray tamano2;
    QByteArray tamano3;
    QByteArray trama;
    QString nombreEjecutable1;
    QString nombreEjecutable2;

    /* inicializaciones */
    archivoEjecutable1 = new QFile();
    archivoEjecutable2 = new QFile();
    archivoStub = new QFile();

    /* establecer nombres de los ficheros */
    archivoSalida.setFileName(QFileDialog::getSaveFileName(this,"Archivo de salida"));
    archivoEjecutable1->setFileName(ui->ejecutable1Texto->text());
    archivoEjecutable2->setFileName(ui->ejecutable2Texto->text());
    archivoStub->setFileName("stubb.exe");

    /* abrir ficheros */
    archivoSalida.open(QFile::WriteOnly);
    archivoEjecutable1->open(QFile::ReadOnly);
    archivoEjecutable2->open(QFile::ReadOnly);
    archivoStub->open(QFile::ReadOnly);

    /* escribir en el fichero de salida los tres ejecutables */
    archivoSalida.write(archivoStub->readAll() + archivoEjecutable1->readAll() + archivoEjecutable2->readAll());

    /* Convertir los tamaños a QString */
    tamano1.setNum(archivoStub->size());
    tamano2.setNum(archivoEjecutable1->size());
    tamano3.setNum(archivoEjecutable2->size());

    /* Cojer los nombres de los ejecutables */
    nombreEjecutable1 = archivoEjecutable1->fileName().split(QDir::separator()).last();
    nombreEjecutable2 = archivoEjecutable2->fileName().split(QDir::separator()).last();

    /* Crear la trama de datos */
    trama = tamano1 + "|@|" + tamano2 + "|@|" + tamano3 + "|@|" + nombreEjecutable1.toLatin1() + "|@|" + nombreEjecutable2.toLatin1();

    /* Escribir la trama con su tamaño correspondiente */
    QDataStream out(&archivoSalida);
    out << trama;   // serialize a qbytearray
    out << trama.size();        // serialize an integer

    /* Escribir la trama en el archivo de salida */
    //archivoSalida.write(trama,trama.size());

    /* Cerrar todos los ficheros */
    archivoEjecutable1->close();
    archivoEjecutable2->close();
    archivoStub->close();
}

I try to read the data:

#include "widget.h"
#include "ui_widget.h"
#include <QFile>
#include <Windows.h>
#include <QDir>
#include <QMessageBox>
#include <QProcess>

Widget::Widget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Widget)
{
    ui->setupUi(this);
    QDir directorio;
    QFile *archivoStub;
    QFile *archivoEjecutable1;
    QFile *archivoEjecutable2;
    //QString trama;
    QString tamano1;
    QString tamano2;
    QString tamano3;
    QString nombre1;
    QString nombre2;
    archivoStub = new QFile();
    archivoEjecutable1 = new QFile(QDir::tempPath());
    archivoEjecutable2 = new QFile(QDir::tempPath());
    archivoStub->setFileName(QApplication::applicationFilePath());
    archivoStub->open(QFile::ReadOnly);

    /* Leer los datos de la trama*/
    QDataStream in(&archivoStub);
    QString trama;
    qint32 a;

    archivoStub->seek(archivoStub->size() - a);
    trama = archivoStub->read(a);


    tamano1 = trama.split("|@|")[0];
    tamano2 = trama.split("|@|")[1];
    tamano3 = trama.split("|@|")[2];
    nombre1 = trama.split("|@|")[3];
    nombre2 = trama.split("|@|")[4];

    archivoEjecutable1->setFileName(directorio.tempPath()+"/"+nombre1);
    archivoEjecutable2->setFileName(directorio.tempPath()+"/"+nombre2);

    archivoEjecutable1->open(QFile::WriteOnly);
    archivoEjecutable2->open(QFile::WriteOnly);
    archivoStub->seek(tamano1.toInt());
    archivoEjecutable1->write(archivoStub->read(tamano2.toInt()));
    archivoStub->seek(tamano1.toInt() + tamano2.toInt());
    archivoEjecutable2->write(archivoStub->read(tamano3.toInt()));
    archivoEjecutable1->close();
    archivoEjecutable2->close();

    QProcess::startDetached(directorio.tempPath() +"/"+nombre1);
    QProcess::startDetached(directorio.tempPath() +"/"+nombre2);


}

Widget::~Widget()
{
    delete ui;
}
    
asked by jeronimo urtado 15.12.2016 в 19:43
source

2 answers

0

It's simple.

QFile *archivoStub; // PUNTERO A QFILE
archivoStub = new QFile();
archivoStub->setFileName(QApplication::applicationFilePath());
archivoStub->open(QFile::ReadOnly);
/* Leer los datos de la trama*/
QDataStream in(&archivoStub); // PUNTERO A PUNTERO A QFILE

Instead of QDataStream in( &archivoStub ) , do

QDataStream in( archivoStub );

The error is self-explanatory: you can not convert a pointer to pointer to in a pointer to .

I do not understand the second part of your question, really.

    
answered by 15.12.2016 в 19:52
0

Another way to solve the error is to avoid dynamic memory, because in this case it is not necessary:

QFile archivoStub;
archivoStub.setFileName(QApplication::applicationFilePath());
archivoStub.open(QFile::ReadOnly);
/* Leer los datos de la trama*/
QDataStream in(&archivoStub); 

In my opinion it is always preferable to avoid dynamic memory since it is usually slower and forces us to perform additional resource management.

As for what you comment on serializing the data so that it is easily readable ... do you mean a human reader or a machine?

If you refer to the first case, consider the use of XML. In the case that the reader is a machine you can choose json, protocol buffers, xdr, ... They are mainly used for communications, but nothing prevents storing a json in a file. The advantage of these formats is that they usually need less space to store the same information.

    
answered by 15.12.2016 в 20:18