Roberto Sepúlveda Bravo Roberto Sepúlveda Bravo - 3 months ago 18
C++ Question

Trying to read lines from file in QT

I'm trying to read lines from a .txt document, the format of the txt is this:




And when I click on the button named "btnVerUsuarios" all the data will be displayed at once in a textBrowser on this order:

Name: theName

Lastname: theLastname

age: theAge

address: theAddress

RUT: theRut

phone: thePhone

And two more times with all the data

This is what I have but when I try to execute this program, an error appears and I have to close the program:

void MainWindow::on_btnVerUsuarios_clicked()
QFile F("datos.txt");;
QTextStream leer(&F); //here we have the content of the txt
QStringList parsear; //to parse the specific data from a line
QString nombres; // to save the name
QString apellidos; // to save the lastname
QString edad; // to save the age
QString address; // to save the address
QString rut; // to save the RUT
QString fono; // to save the phone
QString forma = ""; // to save all the data
QString linea = leer.readLine(); // a line
while (!linea.isNull()) {
parsear = linea.split(";"); // parsing
nombres = parsear[0]; // the names
apellidos = parsear[1]; // the lastnames
edad = parsear[2]; // the age
address = parsear[3]; // the address
rut = parsear[4]; // the RUT
fono = parsear[5]; // the phone

linea=leer.readLine(); //to select the next line (or that is what I want)
forma = forma + "Nombres: "+nombres+"\nApellidos: "+apellidos+"\nEdad: "+edad+"\nDirección: "+address+"\nRUT: "+rut+"\nTeléfono: "+fono+"\n\n";
} // The entire data with the required format is ok, now I have to put it in the textBrowser
ui->textBrowser->setText(forma); //putting the entire data int the textBrowser


if there's another simple way to do this, please help me with it.

Sorry for my bad english and thanks.


I was trying to display all data at once, sorry if I've explained wrong. Anyways, I've solved my question thanks to Afflicted's answer.


Well the most simplistic way to read the entire file and display it all at once would be some thing like

  QFile ReadMyFile("File Path Here");
  QTextStream in(&ReadMyFile);
  QString GetContents = ReadMyFile.readAll();
catch(...) // Generic catch used here, substitute with your own 
   // Print exception if one occurs

You should always have some exception handling laid out especially while working with files you don't want your program to crash right away if there is an issue locating the file etc..

The above code simply reads the entire file into a string and prints it all out into the textBrowser, There are a variety of ways to do this and I'm not sure what you're end goal is and for all intents and purposes this is much more efficient than what you've got above but it's not some thing that you should just copy paste and always use. This is a very simple example to get you on your way, it's intended for massive files...

From what I'm seeing you want to display the entire file at once (which is what you said above), but what I don't stand is why you're trying to split every thing and store it all into various QString?


If you don't want to change your write process and write each field separate and ending with a newline (which i would suggest you do) then the best way would be with a QLinkedList This is some thing that you should use either way if you intend to modify the file..

and it's iterator

It's as simple as reading the file, locating your separating character and adding the next element into the linked list the documentation I've linked will show you every thing you can do with this, hope that helps!