using QTextStream to write in textfile, what is copied after enable the the line to copy is not what I have with qDebug we the line disable
-
Hello,
I want to record temp from sensor and Arduino and I need to add date from theses data.
for that I try:
void MainWindow::readData(const QByteArray &data) { QFile fileOut("d:/Magasin_Temp_Hygro.txt"); fileOut.open(QFile::ReadWrite); QTextStream streamOut(&fileOut); const QChar delimeter = ';' ; QStringList list = streamOut.readAll().split(delimeter); QString line=fileOut.readLine(); streamOut <<data; fileOut.close(); countLine(); } void MainWindow::countLine() { QFile fileOut("d:/Magasin_Temp_Hygro.txt"); fileOut.open(QFile::ReadWrite); int count=0; while (!fileOut.atEnd()) { QString line=fileOut.readLine(); count++; //qDebug()<<"count "<<count; } dating(count); } void MainWindow::dating(int count) { QFile fileOut("d:/Magasin_Temp_Hygro.txt"); fileOut.open(QFile::ReadWrite); QTextStream streamOut(&fileOut); QDateTime timestamp; int count2=0; QString linetowrite; while (!streamOut.atEnd()) { QString line=streamOut.readLine(); count2++; if(count2==count-1) { QStringList text=streamOut.readAll().split(";"); QString time; QString line2; line2=streamOut.readLine(); QString text2=text[0]; timestamp=QDateTime::currentDateTime(); time=timestamp.toString("dd/MM/yy hh'h'mm"); linetowrite=time+text2; } qDebug()<<"linetowrite "<<linetowrite; //streamOut <<linetowrite; } fileOut.close(); }
with this with qDebug, I have on console:
linetowrite "06/05/19 18h30:Temperature:21.9"
but if I remove // from the line at the end of my code on to write the value of linetowrite to the file:
streamOut <<linetowrite;
it's writing on console log and in the file:
06/05/19 18h30:T06/05/19 18h30:Tempe06/05/19 18h30:T06/05/19 18h30:Temperatu06/05/19 18h30:T06/05/19 18h30:Tempe06/05/19 18h30:T06/05/19 18h30:Temperature:206/05/19 18h30:T06/05/19 18h30:Tempe06/05/19 18h30:T06/05/19 18h30:Temperatu06/05/19 18h30:T06/05/19 18h30:Tempe06/05/19 18h30:T06/05/19 18h30:Temperature:21.906/05/19 18h30:T06/05/19 18h30:Tempe06/05/19 18h30:T06/05/19 18h30:Temperatu06/05/19 18h30:T06/05/19 18h30:Tempe06/05/19 18h30:T06/05/19 18h30:Temperature:206/05/19 18h30:T06/05/19 18h30:Tempe06/05/19 18h30:T06/05/19 18h30:Temperatu06/05/19 18h30:T06/05/19 18h30:Tempe06/05/19 18h30:T06/05/19 18h30:Temperature:21.90:Humidi06/05/19 18h30:T06/05/19 18h30:Tempe06/05/19 18h30:T06/05/19 18h30:Temperatu06/05/19 18h30:T06/05/19 18h30:Tempe06/05/19 18h30:T06/05/19 18h30:Temperature:206/05/19 18h30:T06/05/19 18h30:Tempe06/05/19 18h30:T06/05/19 18h30:Temperatu06/05/19 18h30:T06/05/19 18h30:Tempe06/05/19 18h30:T06/05/19 18h30:Temperature:21.906/05/19 18h30:T06/05/19 18h30:Tempe06/05/19 18h30:T06/05/19 18h30:Temperatu06/05/19 18h30:T06/05/19 18h30:Tempe06/05/19 18h30:T06/05/19 18h30:Temperature:206/05/19 18h30:T06/05/19 18h30:Tempe06/05/19 18h30:T06/05/19 18h30:Temperatu06/05/19 18h30:T06/05/19 18h30:Tempe06/05/19 18h30:T06/05/19 18h30:Temperature:21.90:Humidity:206/05/19 18h30:T06/05/19 18h30:Tempe06/05/19 18h30:T06/05/19 18h30:Temperatu06/05/19 18h30:T06/05/19 18h30:Tempe06/05/19 18h30:T06/05/19 18h30:Temperature:206/05/19 18h30:T06/05/19 18h30:Tempe06/05/19 18h30:T06/05/19 18h30:Temperatu06/05/19 18h30:T06/05/19 18h30:Tempe06/05/19 18h30:T06/05/19 18h30:Temperature:21.906/05/19 18h30:T06/05/19 18h30:Tempe06/05/19 18h30:T06/05/19 18h30:Temperatu06/05/19 18h30:T06/05/19 18h30:Tempe06/05/19 18h30:T06/05/19 18h30:Temperature:206/05/19 18h30:T06/05/19 18h30:Tempe06/05/19 18h30:T06/05/19 18h30:Temperatu06/05/19 18h30:T06/05/19 18h30:Tempe06/05/19 18h30:T06/05/19 18h30:Temperature:21.90:Humidi06/05/19 18h30:T06/05/19 18h30:Tempe06/05/19 18h30:T06/05/19 18h30:Temperatu06/05/19 18h30:T06/05/19 18h30:Tempe06/05/19 18h30:T06/05/19 18h30:Temperature:206/05/19 18h30:T06/05/19 18h30:Tempe06/05/19 18h30:T06/05/19 18h30:Temperatu06/05/19 18h30:T06/05/19 18h30:Tempe06/05/19 18h30:T06/05/19 18h30:Temperature:21.906/05/19 18h30:T06/05/19 18h30:Tempe06/05/19 18h30:T06/05/19 18h30:Temperatu06/05/19 18h30:T06/05/19 18h30:Tempe06/05/19 18h30:T06/05/19 18h30:Temperature:206/05/19 18h30:T06/05/19 18h30:Tempe06/05/19 18h30:T06/05/19 18h30:Temperatu06/05/19 18h30:T06/05/19 18h30:Tempe06/05/19 18h30:T06/05/19 18h30:Temperature:21.90:Humidity:29.0006/05/19 18h30:T06/05/19 18h30:Tempe06/05/19 18h30:T06/05/19 18h30:Temperatu06/05/19 18h30:T06/05/19 18h30:Tempe06/05/19 18h30:T06/05/19 18h30:Temperature:206/05/19 18h30:T06/05/19 18h30:Tempe06/05/19 18h30:T06/05/19 18h30:Temperatu06/05/19 18h30:T06/05/19 18h30:Tempe06/05/19 18h30:T06/05/19 18h30:Temperature:21.906/05/19 18h30:T06/05/19 18h30:Tempe06/05/19 18h30:T06/05/19 18h30:Temperatu06/05/19 18h30:T06/05/19 18h30:Tempe06/05/19 18h30:T06/05/19 18h30:Temperature:206/05/19 18h30:T06/05/19 18h30:Tempe06/05/19 18h30:T06/05/19 18h30:Temperatu06/05/19 18h30:T06/05/19 18h30:Tempe06/05/19 18h30:T06/05/19 18h30:Temperature:21.90:Humidi06/05/19 18h30:T06/05/19 18h30:Tempe06/05/19 18h30:T06/05/19 18h30:Temperatu06/05/19 18h30:T06/05/19 18h30:Tempe06/05/19 18h30:T06/05/19 18h30:Temperature:206/05/19 18h30:T06/05/19 18h30:Tempe06/05/19 18h30:T06/05/19 18h30:Temperatu06/05/19 18h30:T06/05/19 18h30:Tempe06/05/19 18h30:T06/05/19 18h30:Temperature:21.906/05/19 18h30:T06/05/19 18h30:Tempe06/05/19 18h30:T06/05/19 18h30:Temperatu06/05/19 18h30:T06/05/19 18h30:Tempe06/05/19 18h30:T06/05/19 18h30:Temperature:206/05/19 18h30:T06/05/19 18h30:Tempe06/05/19 18h30:T06/05/19 18h30:Temperatu06/05/19 18h30:T06/05/19 18h30:Tempe06/05/19 18h30:T06/05/19 18h30:Temperature:21.90:Humidity:206/05/19 18h30:T06/05/19 18h30:Tempe06/05/19 18h30:T06/05/19 18h30:Temperatu06/05/19 18h30:T06/05/19 18h30:Tempe06/05/19 18h30:T06/05/19 18h30:Temperature:206/05/19 18h30:T06/05/19 18h30:Tempe06/05/19 18h30:T06/05/19 18h30:Temperatu06/05/19 18h30:T06/05/19 18h30:Tempe06/05/19 18h30:T06/05/19 18h30:Temperature:21.906/05/19 18h30:T06/05/19 18h30:Tempe06/05/19 18h30:T06/05/19 18h30:Temperatu06/05/19 18h30:T06/05/19 18h30:Tempe06/05/19 18h30:T06/05/19 18h30:Temperature:206/05/19 18h30:T06/05/19 18h30:Tempe06/05/19 18h30:T06/05/19 18h30:Temperatu06/05/19 18h30:T06/05/19 18h30:Tempe06/05/19 18h30:T06/05/19 18h30:Temperature:21.90:Humidi06/05/19 18h30:T06/05/19 18h30:Tempe06/05/19 18h30:T06/05/19 18h30:Temperatu06/05/19 18h30:T06/05/19 18h30:Tempe06/05/19 18h30:T06/05/19 18h30:Temperature:206/05/19 18h30:T06/05/19 18h30:Tempe06/05/19 18h30:T06/05/19 18h30:Temperatu06/05/19 18h30:T06/05/19 18h30:Tempe06/05/19 18h30:T06/05/19 18h30:Temperature:21.906/05/19 18h30:T06/05/19 18h30:Tempe06/05/19 18h30:T06/05/19 18h30:Temperatu06/05/19 18h30:T06/05/19 18h30:Tempe06/05/19 18h30:T06/05/19 18h30:Temperature:206/05/19 18h30:T06/05/19 18h30:Tempe06/05/19 18h30:T06/05/19 18h30:Temperatu06/05/19 18h30:T06/05/19 18h30:Tempe06/05/19 18h30:T06/05/19 18h30:Temperature:21.90:Humidity:29.00"
I don't understand what happen with my code, may be problem with QTextStream ...
Thank you for your help
-
You're writing to a file and after you've written something you read from it and you do this in a loop - what do you expect?
Don't read and write to a file at the same time. -
@Christian-Ehrlicher hello, yes, I have to read the last ligne then add date time to it and write the result to the file
-
-
@jsulm yesy that solve my problem, thanks a lot
I share my last code in case it can help
/**************************************************************************** ** ** Copyright (C) 2012 Denis Shienkov <denis.shienkov@gmail.com> ** Copyright (C) 2012 Laszlo Papp <lpapp@kde.org> ** Contact: https://www.qt.io/licensing/ ** ** This file is part of the QtSerialPort module of the Qt Toolkit. ** ** $QT_BEGIN_LICENSE:BSD$ ** Commercial License Usage ** Licensees holding valid commercial Qt licenses may use this file in ** accordance with the commercial license agreement provided with the ** Software or, alternatively, in accordance with the terms contained in ** a written agreement between you and The Qt Company. For licensing terms ** and conditions see https://www.qt.io/terms-conditions. For further ** information use the contact form at https://www.qt.io/contact-us. ** ** BSD License Usage ** Alternatively, you may use this file under the terms of the BSD license ** as follows: ** ** "Redistribution and use in source and binary forms, with or without ** modification, are permitted provided that the following conditions are ** met: ** * Redistributions of source code must retain the above copyright ** notice, this list of conditions and the following disclaimer. ** * Redistributions in binary form must reproduce the above copyright ** notice, this list of conditions and the following disclaimer in ** the documentation and/or other materials provided with the ** distribution. ** * Neither the name of The Qt Company Ltd nor the names of its ** contributors may be used to endorse or promote products derived ** from this software without specific prior written permission. ** ** ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT ** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR ** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT ** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT ** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE ** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." ** ** $QT_END_LICENSE$ ** ****************************************************************************/ #include "mainwindow.h" #include "ui_mainwindow.h" #include "console.h" #include "settingsdialog.h" #include <QDebug> #include <QLabel> #include <QMessageBox> #include <QSystemTrayIcon> #include <QIcon> #include <QPixmap> #include <QFileInfo> #include <QDateTime> #include <QByteArray> #include <QtCharts/QChartView> #include <QtCharts/QLineSeries> #include <QtCharts/QValueAxis> #include <QtCharts/QCategoryAxis> #include <QtGui/QMouseEvent> #include <QKeyEvent> #include <QStackedWidget> QT_CHARTS_USE_NAMESPACE //! [0] MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), m_ui(new Ui::MainWindow), m_status(new QLabel), m_console(new Console), m_settings(new SettingsDialog), m_serial(new QSerialPort(this)) //! [1] { //! [0] m_ui->setupUi(this); //m_console->setEnabled(false); setCentralWidget(chartView); m_ui->actionConnect->setEnabled(true); m_ui->actionDisconnect->setEnabled(false); m_ui->actionQuit->setEnabled(true); m_ui->actionConfigure->setEnabled(true); m_ui->actionGraph->setEnabled(true); m_ui->statusBar->addWidget(m_status); line_count=0; hygro=0; temp=0; chart->addSeries(temperature); chart->addSeries(hygrometrie); temperature->attachAxis(axisX); temperature->attachAxis(axisYtemp); hygrometrie->attachAxis(axisX); hygrometrie->attachAxis(axisYhygro); axisX->setLabelsAngle(-90); axisX->setStartValue(0); axisX->setRange(0,500); axisX->setLabelsPosition(QCategoryAxis::AxisLabelsPositionOnValue); axisYtemp->setLabelsPosition(QCategoryAxis::AxisLabelsPositionOnValue); axisYtemp->setMin(10); axisYtemp->setMax(50); axisYtemp->setGridLineVisible(true); axisYtemp->setTickCount(10); axisYtemp->setLabelsColor(temperature->pen().color()); axisYhygro->setLabelsPosition(QCategoryAxis::AxisLabelsPositionOnValue); axisYhygro->setMin(10); axisYhygro->setMax(100); axisYhygro->setTickCount(10); axisYhygro->setGridLineVisible(true); axisYhygro->setLabelsColor(hygrometrie->pen().color()); chart->addAxis(axisYhygro, Qt::AlignRight); chart->addAxis(axisYtemp, Qt::AlignLeft); chart->addAxis(axisX, Qt::AlignBottom); chart->setTitle("Revelé Température et humidité Magasin"); chartView->setRenderHint(QPainter::Antialiasing); chartView->chart()->setAxisX(axisX, hygrometrie); chartView->chart()->setAxisX(axisX, temperature); chartView->chart()->setAxisY(axisYtemp, temperature); chartView->chart()->setAxisY(axisYhygro, hygrometrie); chartView->setRenderHint(QPainter::Antialiasing); QFile fileOut("d:/Magasin_Temp_Hygro.txt");//select text file with recorded data fileOut.open(QIODevice::ReadWrite | QIODevice::Text); initActionsConnections(); connect(m_serial, &QSerialPort::errorOccurred, this, &MainWindow::handleError); connect(m_serial, &QSerialPort::readyRead, this, &MainWindow::on_actionGraph); } MainWindow::~MainWindow() { delete m_settings; delete m_ui; } //! [4] void MainWindow::openSerialPort() { const SettingsDialog::Settings p = m_settings->settings(); m_serial->setPortName(p.name); m_serial->setBaudRate(p.baudRate); m_serial->setDataBits(QSerialPort::Data8); m_serial->setParity(QSerialPort::NoParity); m_serial->setStopBits(QSerialPort::OneStop); m_serial->setFlowControl(QSerialPort::NoFlowControl); if (m_serial->open(QIODevice::ReadOnly)) { //m_console->setEnabled(true); // m_console->setLocalEchoEnabled(p.localEchoEnabled); m_ui->actionConnect->setEnabled(false); m_ui->actionDisconnect->setEnabled(true); m_ui->actionConfigure->setEnabled(false); m_ui->actionGraph->setEnabled(true); m_ui->actionData->setEnabled(false); showStatusMessage(tr("Connected to %1 : %2, %3") .arg(p.name).arg(p.stringBaudRate).arg(p.stringDataBits)); } else { QMessageBox::critical(this, tr("Error"), m_serial->errorString()); showStatusMessage(tr("Open error")); } tray = new QSystemTrayIcon(this); icon.addFile(":/images/disconnect.png"); tray->setIcon(icon); tray->setVisible(true); tray->show(); } void MainWindow::closeSerialPort() { if (m_serial->isOpen()) m_serial->close(); m_console->setEnabled(false); m_ui->actionConnect->setEnabled(true); m_ui->actionDisconnect->setEnabled(false); m_ui->actionConfigure->setEnabled(true); showStatusMessage(tr("Disconnected")); } void MainWindow::about() { QMessageBox::about(this, tr("About Simple Terminal"), tr("The <b>Simple Terminal</b> example demonstrates how to " "use the Qt Serial Port module in modern GUI applications " "using Qt, with a menu bar, toolbars, and a status bar.")); } void MainWindow::readData(const QByteArray &data) { QFile fileOut("d:/Magasin_Temp_Hygro.txt"); fileOut.open(QIODevice::ReadWrite | QIODevice::Text); QTextStream streamOut(&fileOut); QString delimiter=QString(data.at(data.size()-1)); if(delimiter==";") { QStringList result=streamOut2.split(";"); QString list = streamOut.readLine(); QDateTime timestamp; timestamp=QDateTime::currentDateTime(); QString time=timestamp.toString("dd/MM/yy hh'h'mm"); streamOut2=time+" "+streamOut2.replace("\n","").replace("\r",""); if(streamOut2.size()<100) {streamOut <<streamOut2.split(";")[0]+"\n";} streamOut2=""; fileOut.copy("d:/Magasin_Temp_Hygro.txt","d:/Temp_Hygro.txt"); }else{ streamOut2=streamOut2+QString(data); } fileOut.close(); } void MainWindow::handleError(QSerialPort::SerialPortError error) { if (error == QSerialPort::ResourceError) { QMessageBox::critical(this, tr("Critical Error"), m_serial->errorString()); closeSerialPort(); } } //! [8] void MainWindow::initActionsConnections() { connect(m_ui->actionConnect, &QAction::triggered, this, &MainWindow::openSerialPort); connect(m_ui->actionDisconnect, &QAction::triggered, this, &MainWindow::closeSerialPort); connect(m_ui->actionQuit, &QAction::triggered, this, &MainWindow::close); //connect(m_ui->actionGraph, &QAction::triggered, this, &MainWindow::closeSerialPort); connect(m_ui->actionGraph, &QAction::triggered, this, &MainWindow::on_actionGraph); connect(m_ui->actionData, &QAction::triggered, this, &MainWindow::toconsole); connect(m_ui->actionData, &QAction::triggered, this, &MainWindow::openSerialPort); connect(m_ui->actionConfigure, &QAction::triggered, m_settings, &SettingsDialog::show); connect(m_ui->actionClear, &QAction::triggered, m_console, &Console::clear); connect(m_ui->actionAbout, &QAction::triggered, this, &MainWindow::about); connect(m_ui->actionAboutQt, &QAction::triggered, qApp, &QApplication::aboutQt); //connect(qApp,SIGNAL(aboutToQuit()),this,SLOT(quitMyApp())); } void MainWindow::keyPressEvent (QKeyEvent *event) { //QKeyEvent *keyEvent = static_cast<QKeyEvent *>(keyEvent); if(event->key()==Qt::Key_Plus) {chartView->chart()->zoomIn();} if(event->key()==Qt::Key_Minus) {chartView->chart()->zoomOut();} if(event->key()==Qt::Key_4) {chartView->chart()->scroll(-10, 0);} if(event->key()==Qt::Key_6) {chartView->chart()->scroll(10, 0);} if(event->key()==Qt::Key_8) {chartView->chart()->scroll(0, 10);} if(event->key()==Qt::Key_2) {chartView->chart()->scroll(0, -10);} } void MainWindow::showStatusMessage(const QString &message) { m_status->setText(message); } void MainWindow::on_actionGraph() { chartView->repaint(); QFile fileOut("d:/Temp_Hygro.txt"); fileOut.open(QIODevice::ReadWrite | QIODevice::Text); while( !fileOut.atEnd())//loop until end of QTextStrem { QString line=fileOut.readLine(); if(line.size()>=51) { date=line.split(":")[0]; bool ok = false; temp=line.split(":")[2].toDouble(&ok); hygro=line.split(":")[4].replace(";","").toDouble(&ok); axisX->append(date,line_count); axisYtemp->append(QString::number(temp),temp); axisYhygro->append(QString::number(hygro),hygro); temperature->append(line_count,temp); qDebug()<<line_count<<" "<<temp; hygrometrie->append(line_count,hygro); } line_count++; } line_count=0; fileOut.close(); fileOut.remove(); const QByteArray data= m_serial->readAll(); readData(data); } void MainWindow::toconsole() { m_ui->actionGraph->setEnabled(true); m_ui->actionData->setEnabled(false); m_console->setEnabled(true); on_actionData(); } void MainWindow::on_actionData() { const QByteArray data= m_serial->readAll(); readData(data); }