Copying the file content
-
@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? -
@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()
). -
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. -
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
-
@suslucoder
QThread has a started signal you connect that signal to your slot -
@suslucoder So, what does copy() return?
-
@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
-
Check if it exists:
qDebug() << "Source exists?" << QFile::exists("C:/Users/ilknu/Documents/MyThread/deneme.txt"); qDebug() << "Destination exists?" << QFile::exists("C:/Users/ilknu/Documents/MyThread/new.txt");
You need to check destination because:
If a file with the name newName already exists, copy() returns false (i.e., QFile will not overwrite it).
-
@suslucoder
Did you read the documentation ofQFile::Copy()
for yourself?Note that if a file with the name newName already exists, copy() returns false (i.e. QFile will not overwrite it).
So, does the destination file already exist? Does the source file also exist?