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. Reading file as written by external process

Reading file as written by external process

Scheduled Pinned Locked Moved Solved General and Desktop
4 Posts 3 Posters 187 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.
  • A Offline
    A Offline
    anthomas
    wrote on 22 Apr 2021, 16:29 last edited by
    #1

    I have a log file that is being written asynchronously from a docker command using a tee. I want to either copy and/or parse the file up to the current point it is written. Is this possible with QFile or at all? Any advice is appreciated.

    1 Reply Last reply
    0
    • C Online
      C Online
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on 22 Apr 2021, 17:49 last edited by
      #2

      What wrong with simply open the file and read it?

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

      1 Reply Last reply
      2
      • M Offline
        M Offline
        mrjj
        Lifetime Qt Champion
        wrote on 22 Apr 2021, 18:07 last edited by
        #3

        Hi and welcome to the forums

        Yes it will work.

        You have to use a timer or similar to read it from time to time
        Disclaimer. fast code. needs more love to be solid.

        class Reader : QObject
        {
            QFile file;
            QTextStream in;
            QTimer timer;
        public:
           explicit Reader(QObject *parent) : QObject(parent)
            {
                connect(&timer, &QTimer::timeout, this, [this]() {
                    while (!in.atEnd()) {
                        QString line = in.readLine();
                        qDebug() << line;
                    }
                });
            }
            void Process(QString filename)
            {
                file.setFileName( filename);
                in.setDevice(&file);
                if ( ! file.open(QFile::ReadOnly)) {
                    qDebug() << "open error" << file.errorString();
                    return;
                }
                timer.start(1000);
                qDebug() << "started";
            }
        };
        
        

        using

         Reader *LogReader = new Reader(this);
        LogReader->Process("e:/test.txt");
        

        Note file must exist when you try.

        alt text

        alt text

        1 Reply Last reply
        4
        • A Offline
          A Offline
          anthomas
          wrote on 29 Apr 2021, 05:23 last edited by
          #4

          Got it working, thank you. Errors were unrelated to the program writing the file.

          1 Reply Last reply
          0

          1/4

          22 Apr 2021, 16:29

          • Login

          • Login or register to search.
          1 out of 4
          • First post
            1/4
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved