简体   繁体   中英

Reading, writing and searching through text files with Qt

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?

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());

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM