Random lines in QFile
-
Hello guys. Here we go again...
I have a Text File and I'm reading from him 3 lines.
QFile file(":/sources/expenses.txt"); { if (file.open(QIODevice::ReadOnly | QIODevice::Text)) { QTextStream in(&file); in.setCodec("UTF-8"); while(!in.atEnd()) { ui->DrawTitle->setText(in.readLine()); ui->DrawDescrip->setText(in.readLine()); ui->DrawCash->setText(in.readLine()); } file.close(); } }I want to read three other, random lines every use of this function. For example:
//TEXT FILE 1 line 2 line 3 line 4 line 5 line 6 lineRunning the read function reads first lines (1 line, 2 line, 3 line) and other one time she reads (4 line, 5 line, 6 line) e.t.c
Can you help me?
-
Hello guys. Here we go again...
I have a Text File and I'm reading from him 3 lines.
QFile file(":/sources/expenses.txt"); { if (file.open(QIODevice::ReadOnly | QIODevice::Text)) { QTextStream in(&file); in.setCodec("UTF-8"); while(!in.atEnd()) { ui->DrawTitle->setText(in.readLine()); ui->DrawDescrip->setText(in.readLine()); ui->DrawCash->setText(in.readLine()); } file.close(); } }I want to read three other, random lines every use of this function. For example:
//TEXT FILE 1 line 2 line 3 line 4 line 5 line 6 lineRunning the read function reads first lines (1 line, 2 line, 3 line) and other one time she reads (4 line, 5 line, 6 line) e.t.c
Can you help me?
@naax If all lines have the same length you can use https://doc.qt.io/qt-5/qfiledevice.html#seek to go to the line you want to read.
Or you simply read whole file into a QStringList where each line is a string, then you can access the lines like you want. -
@naax If all lines have the same length you can use https://doc.qt.io/qt-5/qfiledevice.html#seek to go to the line you want to read.
Or you simply read whole file into a QStringList where each line is a string, then you can access the lines like you want.