【问题标题】:Read a txt file into a QStringList将 txt 文件读入 QStringList
【发布时间】:2013-10-02 16:16:31
【问题描述】:

我有一个包含 3000 个字符串(1 个字符串 - 几个字)的文件。我需要将字符串读入QList。我怎样才能做到这一点?我尝试了以下方法:

1.txt
string
string2

函数() MyList<<"string"<<"string2";

【问题讨论】:

    标签: c++ qt qtcore qfile


    【解决方案1】:

    main.cpp

    #include <QStringList>
    #include <QFile>
    #include <QTextStream>
    #include <QDebug>
    
    int main(int argc, char **argv)
    {
        QString fileName = "foo.txt"; // or "/absolute/path/to/your/file"
        QFile file(fileName);
        if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
            return 1;
    
        QStringList stringList;
        QTextStream textStream(&file);
    
        while (!textStream.atEnd())
            stringList << textStream.readLine();
    
        file.close();
    
        qDebug() << stringList;
    
        return 0;
    }
    

    建筑(类似的东西)

    g++ -fPIC -I/usr/include/qt -I/usr/include/qt/QtCore -lQt5Core main.cpp
    

    输出

    ("string", "string2")
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-12-13
      • 2019-03-26
      • 2015-01-30
      • 1970-01-01
      • 2014-05-06
      • 2012-10-28
      相关资源
      最近更新 更多