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. Copying the file content
QtWS25 Last Chance

Copying the file content

Scheduled Pinned Locked Moved Solved General and Desktop
copyfileqt5file readfile write
32 Posts 6 Posters 7.5k 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 last edited by aha_1980
    #1

    For copyint one txt content, to another txt writing that code script is enough?
    It does not work ı guess there might be somethings else

    QFile::copy(old file path, new file path);
    
    jsulmJ 1 Reply Last reply
    0
    • sierdzioS Offline
      sierdzioS Offline
      sierdzio
      Moderators
      wrote on last edited by
      #2

      Yes, this code is enough to copy a file.

      It will not add contents from source file to destination. It copies the whole file.

      (Z(:^

      1 Reply Last reply
      2
      • D deleted286

        For copyint one txt content, to another txt writing that code script is enough?
        It does not work ı guess there might be somethings else

        QFile::copy(old file path, new file path);
        
        jsulmJ Offline
        jsulmJ Offline
        jsulm
        Lifetime Qt Champion
        wrote on last edited by
        #3

        @suslucoder This copies a file (doesn't matter what it contains).
        If you want us to tell you why it does not work then please provide more information: what exactly is the old and the new paths?

        https://forum.qt.io/topic/113070/qt-code-of-conduct

        D 1 Reply Last reply
        2
        • jsulmJ jsulm

          @suslucoder This copies a file (doesn't matter what it contains).
          If you want us to tell you why it does not work then please provide more information: what exactly is the old and the new paths?

          D Offline
          D Offline
          deleted286
          wrote on last edited by
          #4

          @jsulm ```

          "C:/Users/user/Documents/MyThread/deneme.txt"
          
          
          This is my old file path, the new one is in different name
          jsulmJ 1 Reply Last reply
          0
          • D deleted286

            @jsulm ```

            "C:/Users/user/Documents/MyThread/deneme.txt"
            
            
            This is my old file path, the new one is in different name
            jsulmJ Offline
            jsulmJ Offline
            jsulm
            Lifetime Qt Champion
            wrote on last edited by jsulm
            #5

            @suslucoder And what exactly does not work: it does not copy the file?
            Please post the new apth also, so we can make sure it is not wrong.
            Does the old file exist?
            Also, what does copy return (true/false)?

            https://forum.qt.io/topic/113070/qt-code-of-conduct

            D 1 Reply Last reply
            0
            • jsulmJ jsulm

              @suslucoder And what exactly does not work: it does not copy the file?
              Please post the new apth also, so we can make sure it is not wrong.
              Does the old file exist?
              Also, what does copy return (true/false)?

              D Offline
              D Offline
              deleted286
              wrote on last edited by deleted286
              #6

              @jsulm It does not copy the file. It is in my thread.

              void MyThread::writeData()
              {
              
                  QFile::copy("C:/Users/user/Documents/MyThread/deneme.txt", "C:/Users/user/Documents/MyThread/new.txt");
                  if(!file.open(QIODevice::ReadOnly))
                  {
                      QTextStream in(&file2);
                      while(!in.atEnd())
                      {
                          QString line2=in.readAll();
                          QStringList list = line2.split(QLatin1Char(' '), Qt::SkipEmptyParts);
                          for(const QString &entry : list)
                          {
                              double num = entry.toDouble();
                              queue.enqueue(num);
                              qDebug() << num;
                          }
                      }
                  }
                  qDebug() << "im working in writing thread";
              
                  emit writingDone();
              

              }

              qDebug does not give anytghing on the secreen. And yes the old file exist. I can read its contents

              jsulmJ sierdzioS 2 Replies Last reply
              0
              • D deleted286

                @jsulm It does not copy the file. It is in my thread.

                void MyThread::writeData()
                {
                
                    QFile::copy("C:/Users/user/Documents/MyThread/deneme.txt", "C:/Users/user/Documents/MyThread/new.txt");
                    if(!file.open(QIODevice::ReadOnly))
                    {
                        QTextStream in(&file2);
                        while(!in.atEnd())
                        {
                            QString line2=in.readAll();
                            QStringList list = line2.split(QLatin1Char(' '), Qt::SkipEmptyParts);
                            for(const QString &entry : list)
                            {
                                double num = entry.toDouble();
                                queue.enqueue(num);
                                qDebug() << num;
                            }
                        }
                    }
                    qDebug() << "im working in writing thread";
                
                    emit writingDone();
                

                }

                qDebug does not give anytghing on the secreen. And yes the old file exist. I can read its contents

                jsulmJ Offline
                jsulmJ Offline
                jsulm
                Lifetime Qt Champion
                wrote on last edited by
                #7

                @suslucoder said in Copying the fle content:

                It does not copy the file

                That I already understood.
                Again: what does QFile::copy return?

                I also do not understand what how this file copy is related to the other code:

                if(!file.open(QIODevice::ReadOnly)) // What is file here? What file are you trying to pen here?
                

                https://forum.qt.io/topic/113070/qt-code-of-conduct

                1 Reply Last reply
                0
                • D deleted286

                  @jsulm It does not copy the file. It is in my thread.

                  void MyThread::writeData()
                  {
                  
                      QFile::copy("C:/Users/user/Documents/MyThread/deneme.txt", "C:/Users/user/Documents/MyThread/new.txt");
                      if(!file.open(QIODevice::ReadOnly))
                      {
                          QTextStream in(&file2);
                          while(!in.atEnd())
                          {
                              QString line2=in.readAll();
                              QStringList list = line2.split(QLatin1Char(' '), Qt::SkipEmptyParts);
                              for(const QString &entry : list)
                              {
                                  double num = entry.toDouble();
                                  queue.enqueue(num);
                                  qDebug() << num;
                              }
                          }
                      }
                      qDebug() << "im working in writing thread";
                  
                      emit writingDone();
                  

                  }

                  qDebug does not give anytghing on the secreen. And yes the old file exist. I can read its contents

                  sierdzioS Offline
                  sierdzioS Offline
                  sierdzio
                  Moderators
                  wrote on last edited by
                  #8

                  @suslucoder said in Copying the fle content:

                  qDebug does not give anytghing on the secreen

                  This means that MyThread::writeData() function is not called at all! And that implies the problem lies somewhere else (not in QFile::copy()).

                  (Z(:^

                  D 1 Reply Last reply
                  3
                  • sierdzioS sierdzio

                    @suslucoder said in Copying the fle content:

                    qDebug does not give anytghing on the secreen

                    This means that MyThread::writeData() function is not called at all! And that implies the problem lies somewhere else (not in QFile::copy()).

                    D Offline
                    D Offline
                    deleted286
                    wrote on last edited by
                    #9

                    @sierdzio I've already called it with start() function. Doesnt it started the thread?

                    1 Reply Last reply
                    0
                    • sierdzioS Offline
                      sierdzioS Offline
                      sierdzio
                      Moderators
                      wrote on last edited by
                      #10

                      Yes, start() starts the thread, but it does not mean your writeData() is called. If that were true, you would see your qDebug() output, because it is not in any if conditions - it should be printed every time writeData() is called.

                      (Z(:^

                      D 1 Reply Last reply
                      0
                      • sierdzioS sierdzio

                        Yes, start() starts the thread, but it does not mean your writeData() is called. If that were true, you would see your qDebug() output, because it is not in any if conditions - it should be printed every time writeData() is called.

                        D Offline
                        D Offline
                        deleted286
                        wrote on last edited by deleted286
                        #11

                        @sierdzio yes you are right. I've called the function. Thread is working now. But copying issue does not work.

                        1 Reply Last reply
                        0
                        • sierdzioS Offline
                          sierdzioS Offline
                          sierdzio
                          Moderators
                          wrote on last edited by
                          #12

                          I don't know either, you have not shown any code or stated how you want it all to work... so all I can say is "read the docs" :D QThread has a nice example and detailed description: https://doc.qt.io/qt-5/qthread.html#details

                          (Z(:^

                          D 1 Reply Last reply
                          0
                          • sierdzioS sierdzio

                            I don't know either, you have not shown any code or stated how you want it all to work... so all I can say is "read the docs" :D QThread has a nice example and detailed description: https://doc.qt.io/qt-5/qthread.html#details

                            D Offline
                            D Offline
                            deleted286
                            wrote on last edited by
                            #13

                            @sierdzio I just want, when writeData() thread is called, copy the contents into new txt.

                            J.HilkJ 1 Reply Last reply
                            0
                            • D deleted286

                              @sierdzio I just want, when writeData() thread is called, copy the contents into new txt.

                              J.HilkJ Offline
                              J.HilkJ Offline
                              J.Hilk
                              Moderators
                              wrote on last edited by J.Hilk
                              #14

                              @suslucoder
                              QThread has a started signal you connect that signal to your slot


                              Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


                              Q: What's that?
                              A: It's blue light.
                              Q: What does it do?
                              A: It turns blue.

                              D 1 Reply Last reply
                              3
                              • J.HilkJ J.Hilk

                                @suslucoder
                                QThread has a started signal you connect that signal to your slot

                                D Offline
                                D Offline
                                deleted286
                                wrote on last edited by
                                #15

                                @J-Hilk My thread has working now. But copying does not occur

                                jsulmJ 1 Reply Last reply
                                0
                                • D deleted286

                                  @J-Hilk My thread has working now. But copying does not occur

                                  jsulmJ Offline
                                  jsulmJ Offline
                                  jsulm
                                  Lifetime Qt Champion
                                  wrote on last edited by
                                  #16

                                  @suslucoder So, what does copy() return?

                                  https://forum.qt.io/topic/113070/qt-code-of-conduct

                                  D 1 Reply Last reply
                                  1
                                  • jsulmJ jsulm

                                    @suslucoder So, what does copy() return?

                                    D Offline
                                    D Offline
                                    deleted286
                                    wrote on last edited by
                                    #17

                                    @jsulm it returns nothing, Either true or false

                                    jsulmJ J.HilkJ 2 Replies Last reply
                                    0
                                    • D deleted286

                                      @jsulm it returns nothing, Either true or false

                                      jsulmJ Offline
                                      jsulmJ Offline
                                      jsulm
                                      Lifetime Qt Champion
                                      wrote on last edited by
                                      #18

                                      @suslucoder said in Copying the file content:

                                      it returns nothing, Either true or false

                                      ?
                                      It returns a boolean.
                                      Again: what does it return?!

                                      qDebug() << QFile::copy("C:/Users/user/Documents/MyThread/deneme.txt", "C:/Users/user/Documents/MyThread/new.txt");
                                      

                                      https://forum.qt.io/topic/113070/qt-code-of-conduct

                                      D 1 Reply Last reply
                                      0
                                      • D deleted286

                                        @jsulm it returns nothing, Either true or false

                                        J.HilkJ Offline
                                        J.HilkJ Offline
                                        J.Hilk
                                        Moderators
                                        wrote on last edited by
                                        #19

                                        @suslucoder then the function isn't called.

                                        Please post what you actually have in your code, otherwise no-one will be able to help you


                                        Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


                                        Q: What's that?
                                        A: It's blue light.
                                        Q: What does it do?
                                        A: It turns blue.

                                        1 Reply Last reply
                                        0
                                        • jsulmJ jsulm

                                          @suslucoder said in Copying the file content:

                                          it returns nothing, Either true or false

                                          ?
                                          It returns a boolean.
                                          Again: what does it return?!

                                          qDebug() << QFile::copy("C:/Users/user/Documents/MyThread/deneme.txt", "C:/Users/user/Documents/MyThread/new.txt");
                                          
                                          D Offline
                                          D Offline
                                          deleted286
                                          wrote on last edited by
                                          #20

                                          @jsulm it returns false. Ekran görüntüsü 2021-01-11 111741.png

                                          #include "mythread.h"
                                          #include "mainwindow.h"
                                          #include <QtCore>
                                          #include <QDebug>
                                          #include <QFile>
                                          #include <QTimer>
                                          #include <QThread>
                                          #include <QMutex>
                                          #include <QQueue>
                                          #include <QMessageBox>
                                          #include <QApplication>
                                          #include <QtWidgets/QApplication>
                                          #include <QtWidgets/QMainWindow>
                                          #include <QFileDialog>
                                          #include <QMainWindow>
                                          
                                          QFile file("C:/Users/ilknu/Documents/MyThread/deneme.txt");
                                          
                                          
                                          MyThread::MyThread(QObject* parent)
                                              : QThread(parent)
                                          {
                                              writeData();
                                          }
                                          
                                          MyThread::~MyThread()
                                          {
                                          
                                          }
                                          
                                          void MyThread::writeData()
                                          {
                                              QFile file2("C:/Users/ilknu/Documents/MyThread/new.txt");
                                              QFile::copy("C:/Users/ilknu/Documents/MyThread/deneme.txt","C:/Users/ilknu/Documents/MyThread/new.txt" );
                                              qDebug() << QFile::copy("C:/Users/user/Documents/MyThread/deneme.txt", "C:/Users/user/Documents/MyThread/new.txt");
                                              qDebug() << "im working in writing thread";
                                          
                                              emit writingDone();
                                          
                                          }
                                          
                                          void MyThread::run()  //Reading file from txt with thread1
                                          {
                                          
                                              if (file.open(QIODevice::ReadOnly))
                                              {
                                                  QTextStream in (&file);
                                                  while (!in.atEnd())
                                                  {
                                                      QString line = in.readAll();
                                                      QStringList list = line.split(QLatin1Char(' '), Qt::SkipEmptyParts);
                                                      for(const QString &entry : list)
                                                      {
                                                          double num = entry.toDouble();
                                                          queue.enqueue(num);
                                          
                                                      }
                                          
                                          
                                                      qDebug()<< "im running on worker thread " <<line;
                                          
                                                  } // for
                                              } // while
                                              file.close();
                                          } // if
                                          
                                          JonBJ SGaistS 2 Replies 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