Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Mobile and Embedded
  4. QStream data serialization problem
Forum Updated to NodeBB v4.3 + New Features

QStream data serialization problem

Scheduled Pinned Locked Moved Mobile and Embedded
7 Posts 2 Posters 2.4k 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.
  • G Offline
    G Offline
    GregWilsonLindberg
    wrote on last edited by
    #1

    I'm trying to write a data serializer for some presistent data. I'm using an example from "mimec.org":http://http://www.mimec.org/node/348

    Here is the write startup code:
    @
    bool openForWriting()
    {
    if (false == m_file.open( QIODevice::WriteOnly | QIODevice::Truncate ))
    {
    LOG_ERROR("openForWriting(): '%s'' not opened!!!", QByteArray(QString(m_file.fileName()).toUtf8()).data());

            return false;
        }
    
        m_stream.setDevice( &m_file );
        m_stream.setVersion( QDataStream::Qt_4_8 );
    
        m_stream << MagicHeader;
        m_stream << CurrentVersion;
    
        m_dataVersion = CurrentVersion;
    
        return true;
    }
    

    @

    For now I'm calling this and then closing the file. If the file already exists, then the end result is a zero (0) length file. If I erase the file first, then it works correctly. In my file open I'm asking that an existing file be truncated to zero (0) length, that should have it restart as a zero length file. Is there a problem with truncating a file? Do I need to delete the file explicitly before I start writing, or maybe I need to seek to zero first?

    Also, I'm printing our the file size @m_file.size()@ and the values that I get are basically garbage.

    Any insights will be greatly appreciated.

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

      Hi,

      Are you sure the file is properly closed ? What version of Qt on what OS are you using ?

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

      1 Reply Last reply
      0
      • G Offline
        G Offline
        GregWilsonLindberg
        wrote on last edited by
        #3

        First, I'm running on Qt4.8 Embedded on a BeagleBone Black Angstrom Linux.

        Second, I thought that I was properly closed, I open the file for reading on startup and I print out some messages in the routine that calls close() on the file and I'm seeing the messages so I assumed that the close() call was working.

        I commented out the file read on startup and the write works with an existing zero length file.

        Here is the code that opens for reading:

        @
        bool openForReading()
        {
        if (false == m_file.open( QIODevice::ReadOnly ))
        {
        LOG_ERROR("openForReading(): '%s'' not opened!!!", QByteArray(QString(m_file.fileName()).toUtf8()).data());

                return false;
            }
        
            m_stream.setDevice( &m_file );
            m_stream.setVersion( QDataStream::Qt_4_8 );
        
            qint32 header;
            m_stream >> header;
        
            if ( header != MagicHeader )
            {
                LOG_ERROR("openForReading(): '%s'' bad MagicHeader!!!", QByteArray(QString(m_file.fileName()).toUtf8()).data());
        
                return false;
            }
        
            qint32 version;
            m_stream >> version;
        
            if ( version < MinimumVersion || version > CurrentVersion )
            {
                LOG_ERROR("openForReading(): '%s'' Bad Version:%d!!!", QByteArray(QString(m_file.fileName()).toUtf8()).data(), version);
        
                return false;
            }
        
            m_dataVersion = version;
        
            return true;
        }
        

        @

        For a zero length file the test on the MagicHeader fails and then exits.

        Here is the close() code:

        @
        void close()
        {
        LOG_INFO("WorkingData bytes to write %d", m_file.bytesToWrite());
        LOG_INFO("WorkingData save file size %d", m_file.size());

            m_file.close();
        
            m_dataVersion = -1;
        }
        

        @

        And here is the code that calls everything on startup:

        @
        void loadWorkingData(void)
        {
        if (false == workingDataSave->openForReading())
        {
        LOG_WARNING("WorkingData save file not found!!!");

                workingDataSave->close();
            }
            else
            {
                workingDataSave->stream() >> workingData;
        
                workingDataSave->close();
            }
        }
        

        @

        So if the openForReading() call fails, I call the close() routine that then prints out the messages and calls close() on the file.

        I'll try rearranging things to see if I can get it to work.

        1 Reply Last reply
        0
        • G Offline
          G Offline
          GregWilsonLindberg
          wrote on last edited by
          #4

          I added the file close() call into the openForReading() routine where it returns false with no change in behavior, the write still fails for an existing zero length file.

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

            One thing that might also come in play is that you set the same device again and again on m_stream which is never reseted so there might be an initialization problem there

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

            1 Reply Last reply
            0
            • G Offline
              G Offline
              GregWilsonLindberg
              wrote on last edited by
              #6

              Very good point, I'll look into it.

              Thanks,

              Greg

              1 Reply Last reply
              0
              • G Offline
                G Offline
                GregWilsonLindberg
                wrote on last edited by
                #7

                I tried setting the stream device to 0 after closing the device but that didn't have any effect.

                I then changed the code to create a new dataserializer and therefore a new stream every time I load or save data and that seems to have done the trick, thanks for the tip.

                Greg

                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