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
Forum Updated to NodeBB v4.3 + New Features

Reading file as written by external process

Scheduled Pinned Locked Moved Solved General and Desktop
4 Posts 3 Posters 194 Views 2 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.
  • A Offline
    A Offline
    anthomas
    wrote on 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
    • Christian EhrlicherC Offline
      Christian EhrlicherC Offline
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on 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
      • mrjjM Offline
        mrjjM Offline
        mrjj
        Lifetime Qt Champion
        wrote on 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 last edited by
          #4

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

          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