Copying the file content
-
For copyint one txt content, to another txt writing that code script is enough?
It does not work ı guess there might be somethings elseQFile::copy(old file path, new file path);
-
Yes, this code is enough to copy a file.
It will not add contents from source file to destination. It copies the whole file.
-
@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? -
@jsulm ```
"C:/Users/user/Documents/MyThread/deneme.txt"
This is my old file path, the new one is in different name
-
jsulm Lifetime Qt Championreplied to deleted286 on 11 Jan 2021, 07:00 last edited by jsulm 1 Nov 2021, 07:00
@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)? -
@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
-
@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?
-
@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 inQFile::copy()
). -
@sierdzio I've already called it with start() function. Doesnt it started the thread?
-
Yes,
start()
starts the thread, but it does not mean yourwriteData()
is called. If that were true, you would see yourqDebug()
output, because it is not in anyif
conditions - it should be printed every timewriteData()
is called. -
@sierdzio yes you are right. I've called the function. Thread is working now. But copying issue does not work.
-
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
-
@sierdzio I just want, when writeData() thread is called, copy the contents into new txt.
-
J.Hilk Moderatorsreplied to deleted286 on 11 Jan 2021, 07:46 last edited by J.Hilk 1 Nov 2021, 07:46
@suslucoder
QThread has a started signal you connect that signal to your slot -
@J-Hilk My thread has working now. But copying does not occur
-
@suslucoder So, what does copy() return?
-
@jsulm it returns nothing, Either true or false
-
@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");
-
@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
-
@jsulm it returns false.
#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
5/32