Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Special Interest Groups
  3. QtonPi
  4. QFile and QTextStream on RPI don´t write to File

QFile and QTextStream on RPI don´t write to File

Scheduled Pinned Locked Moved Solved QtonPi
23 Posts 3 Posters 9.7k Views
  • 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.
  • L Offline
    L Offline
    LogiSch17
    wrote on last edited by
    #1

    Hi guys,
    I am just working on a embedded Linux system on a RaspberryPi Compute Module with a small OS build up with buildroot.
    I am using QCanBus and QSerialPort and everything works fine.
    But when I´m tyring to stream some text to file it makes realy strange things.
    I have different parts in my code where i read an write to files.
    For example here i am writing a int to a textfile wich is incressed at every system start , so i can count my startups.

    int HMIController::getStartCount()
    {
        int count=0;
        QString logfile = "/usr/logs/SCount.log";
        QFile file(logfile);
    
        if(file.open(QIODevice::ReadOnly))
        {
            QTextStream in(&file);
            QString line = in.readLine();
            count = line.toInt();
            file.close();
        }
        if(file.open(QIODevice::ReadWrite | QIODevice::Truncate | QIODevice::Text))
        {
            count++;
            QTextStream out(&file);
            out << count ;
            file.close();
        }
        return count;
    }
    

    and this code workes perfectly .
    But i got a other function where I am trying to save RFID keys to a text file:

    void HMIController::logRfid(const QByteArray &token, int authLevel)
    {
        QString filename= "/usr/logs/auth.log";
        QFile file( filename );
        if ( file.open(QIODevice::WriteOnly | QIODevice::Append) )
        {
            QTextStream stream( &file );
            stream << startCount<<":"<<token<<","<<authLevel<< endl;
            file.close();
        }
    }
    

    And this function sometimes only workes one of 5 times , or sometimes i wont work at all.
    I have tried everything.. Only write one char here-> same result
    tried other OpenMethods-> same results
    changed rights of the text file -> same results
    But the QFile always creats the file.. But i seems that QTextStream wont write crorrectly.
    Does someone have a Idea what else i could try?

    greetings
    LogiSch

    kshegunovK 1 Reply Last reply
    0
    • L LogiSch17

      Hi guys,
      I am just working on a embedded Linux system on a RaspberryPi Compute Module with a small OS build up with buildroot.
      I am using QCanBus and QSerialPort and everything works fine.
      But when I´m tyring to stream some text to file it makes realy strange things.
      I have different parts in my code where i read an write to files.
      For example here i am writing a int to a textfile wich is incressed at every system start , so i can count my startups.

      int HMIController::getStartCount()
      {
          int count=0;
          QString logfile = "/usr/logs/SCount.log";
          QFile file(logfile);
      
          if(file.open(QIODevice::ReadOnly))
          {
              QTextStream in(&file);
              QString line = in.readLine();
              count = line.toInt();
              file.close();
          }
          if(file.open(QIODevice::ReadWrite | QIODevice::Truncate | QIODevice::Text))
          {
              count++;
              QTextStream out(&file);
              out << count ;
              file.close();
          }
          return count;
      }
      

      and this code workes perfectly .
      But i got a other function where I am trying to save RFID keys to a text file:

      void HMIController::logRfid(const QByteArray &token, int authLevel)
      {
          QString filename= "/usr/logs/auth.log";
          QFile file( filename );
          if ( file.open(QIODevice::WriteOnly | QIODevice::Append) )
          {
              QTextStream stream( &file );
              stream << startCount<<":"<<token<<","<<authLevel<< endl;
              file.close();
          }
      }
      

      And this function sometimes only workes one of 5 times , or sometimes i wont work at all.
      I have tried everything.. Only write one char here-> same result
      tried other OpenMethods-> same results
      changed rights of the text file -> same results
      But the QFile always creats the file.. But i seems that QTextStream wont write crorrectly.
      Does someone have a Idea what else i could try?

      greetings
      LogiSch

      kshegunovK Offline
      kshegunovK Offline
      kshegunov
      Moderators
      wrote on last edited by
      #2

      Does the function get executed (check with the debugger), is the file opened at all in HMIController::logRfid, as you don't log the (potential) error? What OS (kernel version), Qt (version) and filesystem are you running?

      Read and abide by the Qt Code of Conduct

      1 Reply Last reply
      0
      • L Offline
        L Offline
        LogiSch17
        wrote on last edited by
        #3

        Hi,
        yes the function get´s exectued. When i am changed the stream message to hello world it doesnt work either.
        But i already changed the function to bool where i return true after file.close(); it returns the true.
        I am using this kernel :
        BR2_LINUX_KERNEL_CUSTOM_REPO_URL="https://github.com/raspberrypi/linux.git"
        BR2_LINUX_KERNEL_CUSTOM_REPO_VERSION="d33d0293e245badc4ca6ede3984d8bb8ea63cb1a"

        and QT5:
        BR2_PACKAGE_QT5=y
        BR2_PACKAGE_QT5BASE_LICENSE_APPROVED=y
        BR2_PACKAGE_QT5BASE_CONCURRENT=y
        BR2_PACKAGE_QT5BASE_WIDGETS=y
        BR2_PACKAGE_QT5BASE_EGLFS=y
        BR2_PACKAGE_QT5BASE_FONTCONFIG=y
        BR2_PACKAGE_QT5BASE_GIF=y
        BR2_PACKAGE_QT5BASE_JPEG=y
        BR2_PACKAGE_QT5BASE_PNG=y
        BR2_PACKAGE_QT5SVG=y

        did i missed any package?

        kshegunovK 1 Reply Last reply
        0
        • L LogiSch17

          Hi,
          yes the function get´s exectued. When i am changed the stream message to hello world it doesnt work either.
          But i already changed the function to bool where i return true after file.close(); it returns the true.
          I am using this kernel :
          BR2_LINUX_KERNEL_CUSTOM_REPO_URL="https://github.com/raspberrypi/linux.git"
          BR2_LINUX_KERNEL_CUSTOM_REPO_VERSION="d33d0293e245badc4ca6ede3984d8bb8ea63cb1a"

          and QT5:
          BR2_PACKAGE_QT5=y
          BR2_PACKAGE_QT5BASE_LICENSE_APPROVED=y
          BR2_PACKAGE_QT5BASE_CONCURRENT=y
          BR2_PACKAGE_QT5BASE_WIDGETS=y
          BR2_PACKAGE_QT5BASE_EGLFS=y
          BR2_PACKAGE_QT5BASE_FONTCONFIG=y
          BR2_PACKAGE_QT5BASE_GIF=y
          BR2_PACKAGE_QT5BASE_JPEG=y
          BR2_PACKAGE_QT5BASE_PNG=y
          BR2_PACKAGE_QT5SVG=y

          did i missed any package?

          kshegunovK Offline
          kshegunovK Offline
          kshegunov
          Moderators
          wrote on last edited by
          #4

          I really have no idea what those variables refer to. I was asking about version of the related software. As for packages, you should only need QtCore to work with files (and streams), so no, it doesn't appear for you to have anything missing. Currently my best guess is some kind of buffering either by the kernel or the filesystem ...

          Read and abide by the Qt Code of Conduct

          1 Reply Last reply
          0
          • L Offline
            L Offline
            LogiSch17
            wrote on last edited by
            #5

            ok these variables are out of the buildroot defconfig ..
            the kernel wich is linked is version 4.1.
            and qt5 is the buildroot package wich contains the qtCore.
            After some more tests i think it could be a performance problems. Cause when i let the programm run for 1 min or longer after the function should write to file i works well.
            so 12 out of 15 messages were written.
            When i run the same programm on my linux PC everything works perfectly.

            kshegunovK 1 Reply Last reply
            0
            • L LogiSch17

              ok these variables are out of the buildroot defconfig ..
              the kernel wich is linked is version 4.1.
              and qt5 is the buildroot package wich contains the qtCore.
              After some more tests i think it could be a performance problems. Cause when i let the programm run for 1 min or longer after the function should write to file i works well.
              so 12 out of 15 messages were written.
              When i run the same programm on my linux PC everything works perfectly.

              kshegunovK Offline
              kshegunovK Offline
              kshegunov
              Moderators
              wrote on last edited by
              #6

              Most linuxes (ext* filesystems) will defer flushing the journal (especially if the hardware is slow), so my previous supposition stands and it appears that might be the case here. What's the filesystem on your device?

              Read and abide by the Qt Code of Conduct

              1 Reply Last reply
              0
              • L Offline
                L Offline
                LogiSch17
                wrote on last edited by
                #7

                it´s an ext 4

                kshegunovK 1 Reply Last reply
                0
                • L LogiSch17

                  it´s an ext 4

                  kshegunovK Offline
                  kshegunovK Offline
                  kshegunov
                  Moderators
                  wrote on last edited by
                  #8

                  You could try passing QIODevice::Unbuffered when opening the file and see if that makes a difference, although I wouldn't rely on that flag for production code.

                  Kind regards.

                  Read and abide by the Qt Code of Conduct

                  1 Reply Last reply
                  0
                  • L Offline
                    L Offline
                    LogiSch17
                    wrote on last edited by
                    #9

                    I already tried the Unbuffered flag, makes nearly no difference for me. Also tried to flush the stream after writing with same result.

                    But tanks a lot for your helpt anyway
                    greetings

                    kshegunovK 1 Reply Last reply
                    0
                    • L LogiSch17

                      I already tried the Unbuffered flag, makes nearly no difference for me. Also tried to flush the stream after writing with same result.

                      But tanks a lot for your helpt anyway
                      greetings

                      kshegunovK Offline
                      kshegunovK Offline
                      kshegunov
                      Moderators
                      wrote on last edited by
                      #10

                      @LogiSch17 said in QFile and QTextStream on RPI don´t write to File:

                      Also tried to flush the stream after writing with same result.

                      This wouldn't work, as the Qt buffers are flushed implicitly when you invoke endl or close the file.

                      I already tried the Unbuffered flag, makes nearly no difference for me.

                      Well, the only other thing I could think of is to try and play a bit with the filesystem's flags, for example (assuming you have SSD storage on the device) passing journal_async_commit option when mounting might make a difference.

                      Read and abide by the Qt Code of Conduct

                      1 Reply Last reply
                      0
                      • L Offline
                        L Offline
                        LogiSch17
                        wrote on last edited by
                        #11

                        The rpi compute module uses a eMMc flash storage. Do you have any suggestions about changing flags for that?
                        Cause i dont know much about this stuff.

                        1 Reply Last reply
                        0
                        • L Offline
                          L Offline
                          LogiSch17
                          wrote on last edited by
                          #12

                          Just realised that when i open the files in vim .. at every postion he had failed to write. the file has a line of
                          ^@^@^@^@^@^@....
                          but i only can see them in vim .. text editor says that file has no common codec .. and when i cat the file it shows only the "good" entries.

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

                            Hi and welcome to devnet,

                            You should also add an explicit error message for when the file fails to open. You might get some clues about what is happening.

                            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
                            • L Offline
                              L Offline
                              LogiSch17
                              wrote on last edited by
                              #14

                              Hi and thanks,
                              I have allready add a debug message on file.open, which never has shown an error . And i message the stream status when the file is open after writing, and QTextStream status() always return 0 for :
                              QTextStream::Ok 0 The text stream is operating normally.
                              So the file is always opened correctly and QTextStream seems to write correctly.
                              Because as i mentioned before, when it doesnt save the right message the file as the same among of chars in the file but it looks like
                              ^@^@^@^@...
                              So as kshegunov mentioned, i looks like the buffer has not been writen to the file ..
                              Maybe it is because i never close my programm proberly or shutdown the system correctly and i do not have the possibility to do so.

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

                                Why can't you shutdown your system properly ?

                                If you indeed always "pull the plug" that won't help keep your system clean and not only the files you write but the rest also.

                                What is your exact use case ?

                                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
                                1
                                • L Offline
                                  L Offline
                                  LogiSch17
                                  wrote on last edited by
                                  #16

                                  Hi,
                                  my device is used as a controller and interface for an small kind of electric vehicle. Which is controlled via CAN Bus.
                                  And the the system is turned on and off via one button which is also the emergency off button, so it cuts of the battery completly.

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

                                    How fast and often will you be writing these files ?
                                    How large should they become ?
                                    Also, should they both survive a hard restart ?

                                    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
                                    • L Offline
                                      L Offline
                                      LogiSch17
                                      wrote on last edited by
                                      #18

                                      These files were opened at every system startup and they are logging the initialisation of Can devices and some serial an i2c devices , as well as the log the rfid key which was used to unlock the machine.
                                      So yes they should survive cause i would need them for debuging .

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

                                        So basically you need a read-only system but be able to write in some files.

                                        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
                                        1
                                        • L Offline
                                          L Offline
                                          LogiSch17
                                          wrote on last edited by
                                          #20

                                          Thanks for that .
                                          So i have now a second partiton for log files wich is read/write and one for filesystem.
                                          Should make the system much safer.

                                          kshegunovK 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