pun*_*sta 3 c++ qt qfile qtcore
我有一个3000字符串的文件(1个字符串 - 几个字).我需要把字符串读成一个QList
.我怎样才能做到这一点?我尝试过以下方法:
1.txt
string
string2
Run Code Online (Sandbox Code Playgroud)
function()<=> MyList<<"string"<<"string2";
#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;
}
Run Code Online (Sandbox Code Playgroud)
g++ -fPIC -I/usr/include/qt -I/usr/include/qt/QtCore -lQt5Core main.cpp
Run Code Online (Sandbox Code Playgroud)
("string", "string2")
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
2703 次 |
最近记录: |