Reputation: 2808
I have a file with 3000 strings (1 string-few words). I need to read the strings into a QList
. How can I do that? I have tried the following:
1.txt
string
string2
function() <=> MyList<<"string"<<"string2";
Upvotes: 3
Views: 3268
Reputation: 53173
#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")
Upvotes: 7