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. QDataStream not writing
Qt 6.11 is out! See what's new in the release blog

QDataStream not writing

Scheduled Pinned Locked Moved Unsolved General and Desktop
9 Posts 3 Posters 819 Views 1 Watching
  • 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.
  • T Offline
    T Offline
    tinashe
    wrote on last edited by
    #1

    hi may you please assist the following code does not work it only ceate and empty file.dat file.
    am using Qt 5.15.2 on windows 10 home 64 bit.

    #include <QCoreApplication>
    #include <QFile>
    #include <QImage>
    #include <QDataStream>
    #include <QDebug>

    int main(int argc, char *argv[])
    {
    QCoreApplication a(argc, argv);

    QFile file("file.dat");
    file.open(QIODevice::WriteOnly);
    QDataStream out(&file);   // we will serialize the data into the file
    out << QString("the answer is ");   // serialize a string
    out << (qint32)42;        // serialize an integer
    
    
    QFile file2("file.dat");
    file2.open(QIODevice::ReadOnly);
    QDataStream in(&file2);    // read the data serialized from the file
    QString str;
    qint32 b;
    in >> str >> b;           // extract "the answer is" and 42
    
    qDebug()<< str << b;
    
    return a.exec();
    

    }

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

      You should close the file before trying to open it again.

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

      T 2 Replies Last reply
      4
      • T Offline
        T Offline
        tinashe
        wrote on last edited by
        #3
        This post is deleted!
        1 Reply Last reply
        0
        • Christian EhrlicherC Christian Ehrlicher

          You should close the file before trying to open it again.

          T Offline
          T Offline
          tinashe
          wrote on last edited by
          #4

          @Christian-Ehrlicher

          thank you it worked

          1 Reply Last reply
          0
          • Christian EhrlicherC Christian Ehrlicher

            You should close the file before trying to open it again.

            T Offline
            T Offline
            tinashe
            wrote on last edited by
            #5

            @Christian-Ehrlicher

            nothing written on bytearray can you please assist

            QFile file("img.png");
            file.open(QIODevice::ReadOnly);
            QDataStream out(&file); // we will serialize the data into the file
            QByteArray data;
            out >> data;
            file.close();
            qDebug()<< data.size();

            Christian EhrlicherC 1 Reply Last reply
            0
            • SGaistS Offline
              SGaistS Offline
              SGaist
              Lifetime Qt Champion
              wrote on last edited by
              #6

              Hi,

              You are not checking that the file opening is successful. The path being relative, it's likely failing.

              Interested in AI ? www.idiap.ch
              Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

              T 1 Reply Last reply
              1
              • T tinashe

                @Christian-Ehrlicher

                nothing written on bytearray can you please assist

                QFile file("img.png");
                file.open(QIODevice::ReadOnly);
                QDataStream out(&file); // we will serialize the data into the file
                QByteArray data;
                out >> data;
                file.close();
                qDebug()<< data.size();

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

                @tinashe Why do you think a png file contains QDateStream encoded data?

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

                T 1 Reply Last reply
                1
                • Christian EhrlicherC Christian Ehrlicher

                  @tinashe Why do you think a png file contains QDateStream encoded data?

                  T Offline
                  T Offline
                  tinashe
                  wrote on last edited by
                  #8

                  @Christian-Ehrlicher

                  I think its my misunderstanding the use if this class I was just looking for a class which can make my life easier in sending picture data using Qudp or QTCP . Thanks

                  1 Reply Last reply
                  0
                  • SGaistS SGaist

                    Hi,

                    You are not checking that the file opening is successful. The path being relative, it's likely failing.

                    T Offline
                    T Offline
                    tinashe
                    wrote on last edited by
                    #9

                    @SGaist
                    Thanks

                    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