PDA

View Full Version : Qt equivalent of C++ code



aesthetically asthmatic
23rd June 2018, 16:20
I'm really new to Qt programming and still learning. I am looking for Qt equivalent of this code:


#include <iostream>
#include <string>
#include <fstream>

using namespace std;

int main() {
int i = 0;
string name[3], age[3];
ifstream FileRead("stu.txt");
while(FileRead >> name[i] >> age[i]) {
cout << name[i] << "\t" << age[i] << endl;
i++;
}
FileRead.close();
return 0;
}


The main function of this code is that I want to read the file word by word and store the words into a particular array of strings.

My file (stu.txt) contains the data:


name1 age1
name2 age2
name3 age3


and I want to store the "name1" into name[0] and "age1" into age[0].

Is there an easy way I can do the same in Qt using QString, QFile etc?

d_stranz
24th June 2018, 17:20
Yes. You will find very similar functionality in QString and QTextStream.