Read a file txt
-
I would like to read the contents of a txt file, despite in my file there is only one line but in the qt it shows me several lines
data_sonde.txt
qt result
my code :
void MainWindow::updateGUI_sonde(QByteArray data_sonde) {
//write data***********************
QFile file("data_sonde.txt");if( file.open(QIODevice::Append)) { QTextStream data(&file); data<<data_sonde; } file.close();
//read data*************************
QStringList line ;
if (!file.open(QFile::ReadOnly | QFile::Text)){
qDebug()<<"File not exists";
}
else {
QTextStream lire_data(&file);
while (!lire_data.atEnd())
{
line =lire_data.readLine().split(' ');qDebug()<<line; } file.close(); }
}
can you help me please
-
Hi
You do
line =lire_data.readLine().split(' ');so it splits the one line into lots of pieces as you split by space
then you do
qDebug()<<line;which asks it to dump the whole list, which is what you see.
try
qDebug()<<line.size();and see how many it was split into.
also you text files seems to have 3 lines all in all , right ?
-
Hi
You do
line =lire_data.readLine().split(' ');so it splits the one line into lots of pieces as you split by space
then you do
qDebug()<<line;which asks it to dump the whole list, which is what you see.
try
qDebug()<<line.size();and see how many it was split into.
also you text files seems to have 3 lines all in all , right ?
@mrjj hello thank you for your reply i try
qDebug()<<line.size();
1
1
1
1
3
4
5
6
7
7
8
8
8
8
8
1
8
3
8
3
8
4
8
5
8
6
8
7
8
8
8
8
8
8
8
8
8
8
1
8
8
3
8
8
4
8
8
5
8
8
6
8
8
7
8
8
8
8
8
8 -
Hi
It kinda looks like it loops more than i expect for 3 lines.
could you try list when it actually reads ?
while (!lire_data.atEnd())
{
qDebug() << lire_data.readLine();
}(we do no splitting and simply dump each line)
-
Hi
It kinda looks like it loops more than i expect for 3 lines.
could you try list when it actually reads ?
while (!lire_data.atEnd())
{
qDebug() << lire_data.readLine();
}(we do no splitting and simply dump each line)
@mrjj said in Read a file txt:
while (!lire_data.atEnd())
{
qDebug() << lire_data.readLine();
}thank you for your reply but it's the same result
-
@mrjj said in Read a file txt:
while (!lire_data.atEnd())
{
qDebug() << lire_data.readLine();
}thank you for your reply but it's the same result
@LatitudeFr
hi
How is that possible ?
If you don't split to a list - it should show what it reads from the file directly and
not as many as before.Do you call that code in a loop or something ?
-
@LatitudeFr
hi
How is that possible ?
If you don't split to a list - it should show what it reads from the file directly and
not as many as before.Do you call that code in a loop or something ?
@mrjj the code :
void MainWindow::updateGUI_sonde(QByteArray data_sonde) {
//ecrire les données***********************
QFile file("data_sonde.txt");
QStringList line_data ;if( file.open(QIODevice::Append)) { QTextStream data(&file); data<<data_sonde; file.close();
//lire les données*************************
QStringList line ;
if (!file.open(QFile::ReadOnly | QFile::Text)){
qDebug()<<"File not exists";
}
else {
QTextStream lire_data(&file);
while (!lire_data.atEnd())
{qDebug() << lire_data.readLine(); }
file.close();
} }
}
result :
file txt :
-
Hi
ok that i cant explain.Best guess is that you read data from a serial port and call save and load
pr DataReady signal and hence we see it like this.When reading from serial port, all data might not come in one go. And it kinda looks like this but
I can't understand how your text file then looks normal.
(ahh you use append so the end result looks normal )can you show the code around where you call updateGUI_sonde ?
-
@LatitudeFr I still think, there's a loop, or at least multiple calls to
updateGUI_sonde
add a debug() statement there, to test that
void MainWindow::updateGUI_sonde(QByteArray data_sonde) { qDebug() << Q_FUNC_INFO << data_sonde.toHex(' '); // <------ this one //ecrire les données*********************** QFile file("data_sonde.txt"); QStringList line_data ; if( file.open(QIODevice::Append)) { QTextStream data(&file); data<<data_sonde; file.close(); //lire les données************************* QStringList line ; if (!file.open(QFile::ReadOnly | QFile::Text)) { qDebug()<<"File not exists"; } else { QTextStream lire_data(&file); while (!lire_data.atEnd()) { qDebug() << lire_data.readLine(); } file.close(); } } }
-
@mrjj the code :
void MainWindow::updateGUI_sonde(QByteArray data_sonde) {
//ecrire les données***********************
QFile file("data_sonde.txt");
QStringList line_data ;if( file.open(QIODevice::Append)) { QTextStream data(&file); data<<data_sonde; file.close();
//lire les données*************************
QStringList line ;
if (!file.open(QFile::ReadOnly | QFile::Text)){
qDebug()<<"File not exists";
}
else {
QTextStream lire_data(&file);
while (!lire_data.atEnd())
{qDebug() << lire_data.readLine(); }
file.close();
} }
}
result :
file txt :
@LatitudeFr
Salut,
Utilise les chevrons <> pour rendre le code plus lisible.void MainWindow::updateGUI_sonde(QByteArray data_sonde) { //ecrire les données*********************** QFile file("data_sonde.txt"); QStringList line_data ; if( file.open(QIODevice::Append)) { QTextStream data(&file); data<<data_sonde;
You add/append data over again each time you execute your prog .
-
@LatitudeFr I still think, there's a loop, or at least multiple calls to
updateGUI_sonde
add a debug() statement there, to test that
void MainWindow::updateGUI_sonde(QByteArray data_sonde) { qDebug() << Q_FUNC_INFO << data_sonde.toHex(' '); // <------ this one //ecrire les données*********************** QFile file("data_sonde.txt"); QStringList line_data ; if( file.open(QIODevice::Append)) { QTextStream data(&file); data<<data_sonde; file.close(); //lire les données************************* QStringList line ; if (!file.open(QFile::ReadOnly | QFile::Text)) { qDebug()<<"File not exists"; } else { QTextStream lire_data(&file); while (!lire_data.atEnd()) { qDebug() << lire_data.readLine(); } file.close(); } } }
@J-Hilk thank you for your reply
the result :
can i send you the code by e-mail ? -
Hi
ok that i cant explain.Best guess is that you read data from a serial port and call save and load
pr DataReady signal and hence we see it like this.When reading from serial port, all data might not come in one go. And it kinda looks like this but
I can't understand how your text file then looks normal.
(ahh you use append so the end result looks normal )can you show the code around where you call updateGUI_sonde ?
@mrjj the code :
connect(_sonde, &sonde::gotNewData_sonde, this, &MainWindow::updateGUI_sonde);
signals:
void gotNewData_sonde(QByteArray data_sonde );//prototype du signal
void sonde::newData_sonde() {emit gotNewData_sonde(_sonde.readAll()); // lire tout les donnes dans le port serie
}
-
@J-Hilk thank you for your reply
the result :
can i send you the code by e-mail ?@LatitudeFr no need, you need to accumulate your data.
like @mrjj said, your serial port(?) doesn't notify you when all data arrived, it does when any data arrived.
and you have to store that and decide when and how all data arrived.
from what you showed, I would say your message ends with a
0x2320
aka # and space.QByteArray accumulatedData; QByteArray expectedEnd= QByteArray::fromHex("2320"); void MainWindow::updateGUI_sonde(QByteArray data_sonde) { qDebug() << Q_FUNC_INFO << data_sonde.toHex(' '); // <------ this one accumulatedData.append(data_sonde); if(!accumulatedData.endsWith(expectedEnd)) return; //ecrire les données*********************** QFile file("data_sonde.txt"); QStringList line_data ; if( file.open(QIODevice::Append)) { QTextStream data(&file); data<<accumulatedData; file.close(); accumulatedData.clear(); //lire les données************************* QStringList line ; if (!file.open(QFile::ReadOnly | QFile::Text)) { qDebug()<<"File not exists"; } else { QTextStream lire_data(&file); while (!lire_data.atEnd()) { qDebug() << lire_data.readLine(); } file.close(); } } }
-
@LatitudeFr no need, you need to accumulate your data.
like @mrjj said, your serial port(?) doesn't notify you when all data arrived, it does when any data arrived.
and you have to store that and decide when and how all data arrived.
from what you showed, I would say your message ends with a
0x2320
aka # and space.QByteArray accumulatedData; QByteArray expectedEnd= QByteArray::fromHex("2320"); void MainWindow::updateGUI_sonde(QByteArray data_sonde) { qDebug() << Q_FUNC_INFO << data_sonde.toHex(' '); // <------ this one accumulatedData.append(data_sonde); if(!accumulatedData.endsWith(expectedEnd)) return; //ecrire les données*********************** QFile file("data_sonde.txt"); QStringList line_data ; if( file.open(QIODevice::Append)) { QTextStream data(&file); data<<accumulatedData; file.close(); accumulatedData.clear(); //lire les données************************* QStringList line ; if (!file.open(QFile::ReadOnly | QFile::Text)) { qDebug()<<"File not exists"; } else { QTextStream lire_data(&file); while (!lire_data.atEnd()) { qDebug() << lire_data.readLine(); } file.close(); } } }
@J-Hilk it works very well thank you very much but please how I can read just the last line
-
@J-Hilk it works very well thank you very much but please how I can read just the last line
Hi
it doesn't work like that :)Say you send "This is my line" from the board.
then you get more than one read signal and data comes like
Thi
is m
y li
neso you need to add them together to make a line of it.
In this case we know we got all data when we see #there is no last line. it comes in bytes and we make a line from that.
-
Hi
it doesn't work like that :)Say you send "This is my line" from the board.
then you get more than one read signal and data comes like
Thi
is m
y li
neso you need to add them together to make a line of it.
In this case we know we got all data when we see #there is no last line. it comes in bytes and we make a line from that.
@mrjj with this code the problem is solved but I would need to read every time I press the button just the last line and not all the lines
the code :
QByteArray accumulatedData;
QByteArray expectedEnd= QByteArray::fromHex("2320");
void MainWindow::updateGUI_sonde(QByteArray data_sonde) {
// qDebug() << Q_FUNC_INFO << data_sonde.toHex(' '); // <------ this one
accumulatedData.append(data_sonde);if(!accumulatedData.endsWith(expectedEnd)) return; //ecrire les données*********************** QFile file("data_sonde.txt"); QStringList line_data ; if( file.open(QIODevice::Append)) { QTextStream data(&file); data<<accumulatedData; file.close(); accumulatedData.clear();
//lire les données*************************
QStringList line ;
if (!file.open(QFile::ReadOnly | QFile::Text)) {
qDebug()<<"File not exists";
} else {
QTextStream lire_data(&file);
while (!lire_data.atEnd()) {
line =lire_data.readLine().split(' ');
if(!line.contains(("para")))
{
if(!line.contains(("data")))
{
if(!line.contains(("#")))
{bool ok; float ph =line.at(0).toDouble(&ok); if(!ok) continue; qDebug()<<ph; ui->sonde_label->setNum(ph); qDebug() << line; } } } } file.close(); }
}
}the file.txt
the result
-
Hi
Since you now wait until you see #if(!accumulatedData.endsWith(expectedEnd)) return;
then you might want to use
file.open(QIODevice::Read | QIODevice::Truncate | QIODevice::Text);
and not
file.open(QIODevice::Append)so you only save the complete line once. Else if you test more than once and don't delete the file between,
it will have more than 1 data line /set. Like you show in the notepad image. -
Hi
Since you now wait until you see #if(!accumulatedData.endsWith(expectedEnd)) return;
then you might want to use
file.open(QIODevice::Read | QIODevice::Truncate | QIODevice::Text);
and not
file.open(QIODevice::Append)so you only save the complete line once. Else if you test more than once and don't delete the file between,
it will have more than 1 data line /set. Like you show in the notepad image.@mrjj thank your for your reply but i need file.open(QIODevice::Append) to write on the file
for the first time it reads only one data since in our file there is only one data but when I press the button for the second time it reads 2 data since we have 2 data in the file but I would need to read just the last datafor the first time
for the second time
-
@mrjj thank your for your reply but i need file.open(QIODevice::Append) to write on the file
for the first time it reads only one data since in our file there is only one data but when I press the button for the second time it reads 2 data since we have 2 data in the file but I would need to read just the last datafor the first time
for the second time
Why ?
it will only write a full line since we wait until we see #
so when your load part runs, then it can load the full line.
Using append when opening the files allows appending more to it next time.
but unless board sent many different data lines and you only want the last one, I'm not sure why
you need append ?(update) Ahh. you do want to send more than one data line and store all in a file but then later
only use the last data ? -
QByteArray accumulatedData; QByteArray expectedEnd= QByteArray::fromHex("2320"); void MainWindow::updateGUI_sonde(QByteArray data_sonde) { qDebug() << Q_FUNC_INFO << data_sonde.toHex(' '); // <------ this one accumulatedData.append(data_sonde); if(!accumulatedData.endsWith(expectedEnd)) return; //ecrire les données*********************** QFile file("data_sonde.txt"); QStringList line_data ; if( file.open(QIODevice::Append)) { QTextStream data(&file); data<<accumulatedData; file.close(); //lire les données************************* QStringList line ; if (!file.open(QFile::ReadOnly | QFile::Text)) { qDebug()<<"File not exists"; } else { QTextStream lire_data(&file); lire_data.seek(file.size() - accumulatedData.size()); while (!lire_data.atEnd()) { qDebug() << lire_data.readLine(); } file.close(); } } accumulatedData.clear(); //We now clear at the very end }