[SOLVED]Fread convert to qt



  • I have code in c:
    @
    FILE *f;
    _ENT rec; (_ENT is a my structur)
    fread (&rec,1,sizeof(rec),f)
    @

    how to convert code in code qt??



  • Hey, ~Salvatello!

    I suppose, you should read some documentation about QFile and QTextStream. If I understand you correctly, the problem you want to solve is trivial and requires basic stream reading from the file.

    Good luck!



  • I have a struct:
    @typedef struct {
    unsigned short idrec;
    char dir;
    unsigned char flag;
    double x1,
    y1,
    x2,
    y2,
    cx,
    cy,
    raggio;
    } _ENT;@

    and FILE *f;
    _ENT rec;

    then i have:
    @fread (&rec,1,sizeof(rec),f);
    if (rec.idrec != AAAA)
    {
    printf("ERROR");
    }
    ....
    ...
    ...
    @

    how is this translated code in qt.
    How can I read from a file byte of a certain size and save them in the struct _ENT ????


  • Moderators

    see "QDataStream":http://qt-project.org/doc/qt-4.8/qdatastream.html#details
    this page should contain all you need.

    You could implement the data stream operators (<<) for your _ENT struct.



  • ho risolto così:
    @ QFile filefile(percorsoFile);
    filefile.open(QIODevice::ReadOnly);
    filefile.read((char*)&rec,sizeof(rec));
    qDebug()<<rec.idrec;
    qDebug()<<rec.dir;
    qDebug()<<rec.flag;
    qDebug()<<rec.x1;
    qDebug()<<rec.x2;
    qDebug()<<rec.y1;
    qDebug()<<rec.y2;
    qDebug()<<rec.cx;
    qDebug()<<rec.cy;
    qDebug()<<rec.raggio;
    filefile.close();@


Log in to reply
 

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