PDA

View Full Version : Read with one thread, send datas to another thread



suslucoder
10th January 2021, 16:42
Im working with threads. In the following code, i read datas from txt and run it on my thread. I want to use another thread for writing the datas another txt file. How can i do it?

mythread.cpp



#include "mythread.h"
#include <QtCore>
#include <QDebug>
#include <QFile>
MyThread::MyThread()
{

}

void MyThread::run() //Reading file from txt with thread1
{
QFile file("C:/Users/ilknu/Documents/untitled1/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()<<num;
queue.enqueue(num);

} // for
} // while
} // if

file.close();
}

d_stranz
10th January 2021, 22:05
There is no reason to use threads for this. Open both files, read from one file, compute your sum, and write it to the second file. When the reading file is at the end, close both files.

ChristianEhrlicher
11th January 2021, 16:05
There is no reason to use threads for this.
We try to explain him this for a while - now he tries it in another forum :D

d_stranz
11th January 2021, 17:43
Maybe his toolbox is filled only with hammers, so every problem becomes a nail.