Reading, writing and searching through text files with Qt

Go To StackoverFlow.com

0

I'm stuck with Qt (again) and I'm trying to read text from a text file. Here is what the text file contains:

1001 James Bark
1002 Jeremy Parker
1003 Seinfeld Parker
1004 Sigfried FonStein
1005 Rabbun Hassan
1006 Jenniffer Jones
1007 Agent Smith
1008 Mister Anderson

Don't ask where the names came from. I need to be able to index this file and add it to a table. The table, as of now, looks like this:

|--------------------------------|
|          |First Name|Last Name | 
|--------------------------------|
|1001      |          |          |  
|--------------------------------| 
|          |          |          |
|--------------------------------|      
|          |          |          |
|--------------------------------|      
|          |          |          |      
|--------------------------------|

But it needs to look like this

    |--------------------------------|
    |          |First Name|Last Name | 
    |--------------------------------|
    |1001      |James     |Bark      |  
    |--------------------------------| 
    |1002      |Jeremy    |Parker    |
    |--------------------------------|      
    |1003      |Seinfeld  |FonStein  |
    |--------------------------------|      
    |1004      |Rabbun    |Hassan    |      
    |--------------------------------|

The table part and putting the items in is not a problem. The problem lies in reading a text file that is formatted like the example at the top. Also, I don't need it to be formatted like that. I could have 3 different text files with ID, Fname, and Lname seperated in those files. This would be a hassle, but if I have to do this I will.

So my question is how the hell do I index through this file and get these values?

Here's a function:

Function(){
    QString fileName = "C:\\Users\\Gabe\\SeniorProj\\Students.txt";
    QFile mFile(fileName);

    if(!mFile.open(QFile::ReadOnly | QFile::Text)){
        qDebug() << "Could not open file for reading";
        return;
    }

    QTextStream in(&mFile);
    QString mText = in.readAll();

    mFile.flush();
    mFile.close();
    }

This code I got from some tutorial somewhere. Everything works, I've checked if I can access the file, and all is good. But what to do with it... I have a string equal to all of the text. That's not very useful. So I tried stuff I used in c++, like mFile.nextInt(); Well that function doesn't exist!!!

How do I search through a text file for specific things in Qt?

2012-04-05 20:10
by Gabriel


5

That file format will cause you a lot of trouble if the names can contain spaces, because you cannot know whether the first name or the last contains two parts. Consider using some other character as a separator, for example:

1001;James;Bark
1002;Jeremy;Parker
1003;Seinfeld;Parker

Then read the lines one at a time (see QTextStream's documentation) and split the QString to QStringList:

QTextStream stream(stdin);
QString line;    
do {
    line = stream.readLine();
    QStringList parts = line.split(";", QString::KeepEmptyParts);
    if (parts.length() == 3) {
        QString id        = parts[0];
        QString firstName = parts[1];
        QString lastName  = parts[2];
    }        
} while (!line.isNull());
2012-04-05 21:05
by NoName
This really helped, it was exactly what I needed. Thank you so much ^_ - Gabriel 2012-04-06 17:49
Ads