简体   繁体   English

从Qt C ++中的文件中读取2D数组

[英]Read in 2D array from file in Qt C++

I have a comboBox in a Qt GUI in mainwindow.cpp. 我在mainwindow.cpp的Qt GUI中有一个comboBox。 I have this text file with a list of emails and employee names like this (this is just an example file): 我有一个文本文件,其中包含电子邮件和员工姓名的列表,如下所示(这只是一个示例文件):

Pete White      pete@mail.com
Jack Peterson   jack@mail.com
Mike Jackson    mike@mail.com
Dave Middleton  dave@mail.com
Sylvester Brown sylvester@mail.com

So the ComboBox should display these names but NOT the emails so I guess I could read this file in as a 2D array. 因此,ComboBox应该显示这些名称,而不显示电子邮件,因此我想我可以将其作为2D数组读取。 (I do know how to do this in C but not in C++ or Qt) And then I only need the first column. (我确实知道如何在C中执行此操作,但在C ++或Qt中却不行)然后我只需要第一列。

This comboBox is used in a simple form application. 此comboBox在简单的表单应用程序中使用。 So if this form is filled in, it puts these fills in a variable. 因此,如果填写此表单,则会将这些填充放入变量中。 So like this Subject = "Soccer" . 像这样Subject = "Soccer" All the other inputs worked (like subject) BUT with this one its different. 所有其他输入(像主题一样)都起作用,但与此不同。 If Pete White is selected, it should not be email = "Pete White" , but email = "pete@mail.com" . 如果选择了Pete White ,则不应为email = "Pete White" ,而应为email = "pete@mail.com"

This maybe explained quiet vague but thanks for helping me :) It might help if you guys have something visual: 也许这可以解释得很安静,但是感谢您的帮助:)如果你们有视觉效果,这可能会有所帮助:
在此处输入图片说明


*It's Dutch so don't mind the other the text in the image :) *是荷兰语,所以不要介意图片中的其他文字:)

You didn't specify, so I am assuming that this file has fixed-width items, so name is 15 characters long, and the rest of the line is email address. 您没有指定,所以我假设此文件具有固定宽度的项目,因此名称为15个字符长,其余部分为电子邮件地址。

Now, you want to read the file, split line, create QPair<String, String> and put it into the QList : 现在,您想读取文件,分割行,创建QPair<String, String>并将其放入QList

int main(int argc, char *argv[])
{
    QList<QPair<QString, QString> > lst;

    QFile inputFile("addresses.txt");

    if (inputFile.open(QIODevice::ReadOnly))
    {
       QTextStream in(&inputFile);
       while ( !in.atEnd() )
       {
          QString line = in.readLine();
          // use mid to split line - but you could use QString::split to split on character
          lst.append(qMakePair(line.mid(0, 15).trimmed(), line.mid(15).trimmed()));
       }
    }

    inputFile.close();


    for(int i=0; i< lst.count(); i++)
    {
        std::cout << "Name: " << qPrintable(lst[i].first) << " Address: " << qPrintable(lst[i].second) << std::endl;
    }


}

You can read file to QStringList and remove or store email if needed like this: 您可以将文件读取到QStringList并根据需要删除或存储电子邮件,如下所示:

QFile file("path_to_file");
file.open(QIODevice::ReadOnly);
// reads file, splits it by newline symbol and puts it to qstringlist
QStringList list=QString(file.readAll()).split(QRegExp("[\r\n]"),QString::SkipEmptyParts);
for (int i=0;i<list.count();i++){
    list[i]=list[i].simplified(); // removing unneeded spaces inside string
    list[i].remove(list.at(i).section(" ",-1)); // removing word following the last space, where email is in example
    //(if email is needed, just store it somewhere before removing)
    // list.at(i).section(" ",-1) - email
    // list.at(i).section(" ",0,list.at(i).count()-1) -name and surname
}
file.close();

more details about section is here 有关该部分的更多详细信息在这里

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

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