gpt4 book ai didi

c++ - 将 txt 文件读入 QStringList

转载 作者:太空狗 更新时间:2023-10-29 20:26:37 25 4
gpt4 key购买 nike

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

1.txt
string
string2

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

最佳答案

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")

关于c++ - 将 txt 文件读入 QStringList,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19141712/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com