简体   繁体   English

使用 Qt 读取、写入和搜索文本文件

[英]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.我被 Qt(再次)困住了,我正在尝试从文本文件中读取文本。 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.我可以有 3 个不同的文本文件,这些文件中的 ID、Fname 和 Lname 是分开的。 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:

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();所以我尝试了我在 c++ 中使用的东西,比如mFile.nextInt(); Well that function doesn't exist!!!那么 function 不存在!!!

How do I search through a text file for specific things in Qt?如何在文本文件中搜索 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 的文档)并将 QString 拆分为 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());

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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