Copying the file content
-
wrote on 11 Jan 2021, 06:54 last edited by aha_1980 1 Nov 2021, 07:11
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.
-
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);
@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 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?wrote on 11 Jan 2021, 06:58 last edited by@jsulm ```
"C:/Users/user/Documents/MyThread/deneme.txt"
This is my old file path, the new one is in different name
-
@jsulm ```
"C:/Users/user/Documents/MyThread/deneme.txt"
This is my old file path, the new one is in different name
Lifetime Qt Championwrote 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)? -
@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)?wrote on 11 Jan 2021, 07:02 last edited by deleted286 1 Nov 2021, 07:03@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
-
@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?
-
@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:
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()
). -
@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()
).wrote on 11 Jan 2021, 07:32 last edited by@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. -
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.wrote on 11 Jan 2021, 07:40 last edited by deleted286 1 Nov 2021, 07:43@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
-
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
wrote on 11 Jan 2021, 07:45 last edited by@sierdzio I just want, when writeData() thread is called, copy the contents into new txt.
-
@sierdzio I just want, when writeData() thread is called, copy the contents into new txt.
@suslucoder
QThread has a started signal you connect that signal to your slot -
@suslucoder
QThread has a started signal you connect that signal to your slotwrote on 11 Jan 2021, 08:09 last edited by@J-Hilk My thread has working now. But copying does not occur
-
@J-Hilk My thread has working now. But copying does not occur
@suslucoder So, what does copy() return?
-
@suslucoder So, what does copy() return?
wrote on 11 Jan 2021, 08:15 last edited by@jsulm it returns nothing, Either true or false
-
@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");
-
@jsulm it returns nothing, Either true or false
@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
-
@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");
wrote on 11 Jan 2021, 08:18 last edited by@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
2/32