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. How can read input from a txt and write them to another txt file?
QtWS25 Last Chance

How can read input from a txt and write them to another txt file?

Scheduled Pinned Locked Moved Unsolved General and Desktop
qt5readingthreads
5 Posts 4 Posters 898 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.
  • D Offline
    D Offline
    deleted286
    wrote on 10 Jan 2021, 11:10 last edited by
    #1

    I have 2 txt file. One is for reading and the other one is writing. They should be in different threads. How can i write the data i read to another file. Here is my code for reading, queue part can be skip. I dont need it

    Here is my writing thread

    void MyThread::writeData()
    {
    ....
        emit writingDone();
    }
    

    Here is the reading part

    void MyThread::run()  //Reading file from txt with thread1
    {
    
        QFile file("C:/Users/ilknu/Documents/MyThread/deneme.txt");
    
        if (file.open(QIODevice::ReadOnly))
        {
            QTextStream in (&file);
            while (!in.atEnd())
            {
    
                QString line = in.readLine();
                QStringList list = line.split(QLatin1Char(' '), Qt::SkipEmptyParts);
                for(const QString &entry : list)
                {
                    double num = entry.toDouble();
                    qDebug()<< "im running on worker thread " <<num;
                    queue.enqueue(num);
    
    
                } // for
            } // while
        } // if
    
        file.close();
    }
    
    J 1 Reply Last reply 10 Jan 2021, 12:18
    0
    • S Offline
      S Offline
      SGaist
      Lifetime Qt Champion
      wrote on 10 Jan 2021, 11:54 last edited by
      #2

      Hi,

      Please, start by learning the base concepts of threading. You are trying to go too fast with very advanced topics without having solid bases to handle them.

      There are several ways to achieve what you want but from the looks of it a basic file copy operation is all that you would need.

      Do not over engineer stuff.

      In any case, start with the beginning.

      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
      3
      • D deleted286
        10 Jan 2021, 11:10

        I have 2 txt file. One is for reading and the other one is writing. They should be in different threads. How can i write the data i read to another file. Here is my code for reading, queue part can be skip. I dont need it

        Here is my writing thread

        void MyThread::writeData()
        {
        ....
            emit writingDone();
        }
        

        Here is the reading part

        void MyThread::run()  //Reading file from txt with thread1
        {
        
            QFile file("C:/Users/ilknu/Documents/MyThread/deneme.txt");
        
            if (file.open(QIODevice::ReadOnly))
            {
                QTextStream in (&file);
                while (!in.atEnd())
                {
        
                    QString line = in.readLine();
                    QStringList list = line.split(QLatin1Char(' '), Qt::SkipEmptyParts);
                    for(const QString &entry : list)
                    {
                        double num = entry.toDouble();
                        qDebug()<< "im running on worker thread " <<num;
                        queue.enqueue(num);
        
        
                    } // for
                } // while
            } // if
        
            file.close();
        }
        
        J Online
        J Online
        JonB
        wrote on 10 Jan 2021, 12:18 last edited by JonB 1 Oct 2021, 14:11
        #3

        @suslucoder
        In the other thread, you agreed (I thought) with @Christian-Ehrlicher and me not to use threads. They are advanced, and very likely not needed.

        Also, please do not do as you have just done and delete that thread of yours where other people have been making posts to help you. On a forum it is customary to leave a topic in existence, not to delete it. It can be useful to others, and certainly it is relevant to this new topic you have started now.

        [EDIT Either I'm going mad and you did not delete it, or you have restored the other topic now, in which case thank you! Either way it's good :) ]

        D C 2 Replies Last reply 10 Jan 2021, 16:12
        0
        • J JonB
          10 Jan 2021, 12:18

          @suslucoder
          In the other thread, you agreed (I thought) with @Christian-Ehrlicher and me not to use threads. They are advanced, and very likely not needed.

          Also, please do not do as you have just done and delete that thread of yours where other people have been making posts to help you. On a forum it is customary to leave a topic in existence, not to delete it. It can be useful to others, and certainly it is relevant to this new topic you have started now.

          [EDIT Either I'm going mad and you did not delete it, or you have restored the other topic now, in which case thank you! Either way it's good :) ]

          D Offline
          D Offline
          deleted286
          wrote on 10 Jan 2021, 16:12 last edited by
          #4

          @JonB I did not delete, since your last warning

          1 Reply Last reply
          0
          • J JonB
            10 Jan 2021, 12:18

            @suslucoder
            In the other thread, you agreed (I thought) with @Christian-Ehrlicher and me not to use threads. They are advanced, and very likely not needed.

            Also, please do not do as you have just done and delete that thread of yours where other people have been making posts to help you. On a forum it is customary to leave a topic in existence, not to delete it. It can be useful to others, and certainly it is relevant to this new topic you have started now.

            [EDIT Either I'm going mad and you did not delete it, or you have restored the other topic now, in which case thank you! Either way it's good :) ]

            C Online
            C Online
            Christian Ehrlicher
            Lifetime Qt Champion
            wrote on 10 Jan 2021, 16:44 last edited by
            #5

            @JonB said in How can read input from a txt and write them to another txt file?:

            Either I'm going mad and you did not delete it

            Sorry, I restored it without any notice here :)

            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
            1

            2/5

            10 Jan 2021, 11:54

            • Login

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