Reading .txt file info into variables



  • How do I read in a text file and assign the integers in that file to my own variables?



  • That depends, as always :-)
    I have no oidea, how the structure of the file is.
    Let's gess, one line contains one integer.
    Then you could just read the whole file into a QStringList, iterate of the entries and call toINt on each of them.



  • Thank you very much Gerolf. I found this code snippet which supposedly puts my text file into a QStringList

    @QStringList stringList;
    QFile textFile("test.txt");
    QTextStream textStream(&textFile);
    while (true)
    {
    QString line = textStream.readLine();
    if (line.isNull())
    break;
    else
    stringList.append(line);
    }@

    If that is done correctly, how do I go about iterating through it?



  • I have QStringList of integers that I read in from a file. How do I iterate through this list to: a) save each value b) cast each to an integer.


  • Moderators

    I'm not sure what you mean by "save each value" but iterating through a QStringList is fairly simple. There are a number of ways to do so.

    @
    QStringList list;

    // iterate through each element of list (which will be accessible via the QString "str")
    foreach (QString str, list)
    {
    ...
    int value = str.toInt(); // get integer representation of str
    ...
    }
    @

    BTW, This stuff is very well-documented.



  • I meant to say store each into a variable, which you answered. However I'm trying to do so in the following code, and when I try to print the value, nothing is displayed. Am I reading my text file into the QStringList incorrectly?

    @int main()
    {
    QStringList stringList;
    QFile textFile("test.txt");
    QTextStream textStream(&textFile);
    while (true)
    {
    QString line = textStream.readLine();
    if (line.isNull())
    break;
    else
    stringList.append(line);
    }

    foreach(QString str, stringList)
    {
        int value = str.toInt();
        cout << value;
    }
    return 0;
    

    }
    @


  • Moderators

    You need to call "textFile.open()":http://doc.qt.nokia.com/4.7-snapshot/qfile.html#open

    Be sure and check its return value. There's an example in the QFile docs.


  • Moderators

    A cleaner implementation would probably be:

    @
    QStringList stringList;
    QFile textFile("test.txt");
    if (textFile.open(QIODevice::ReadOnly)
    {
    QTextStream textStream(&textFile);
    while (!textStream.atEnd())
    {
    stringList.append(textStream.readLine());
    }
    }
    @

    Caveat: Brain to keyboard. Untested code. Not responsible for silly errors. YMMV



  • that worked! thank you very much!



  • [quote author="sheehan1234" date="1317152090"]
    If that is done correctly, how do I go about iterating through it?[/quote]

    @
    foreach(const QString line, stringList) {
    // do something with the line
    }
    @



  • This discussion looks a lot like "this":http://developer.qt.nokia.com/forums/viewthread/10146/ one...



  • [quote author="Andre" date="1317189187"]This discussion looks a lot like "this":http://developer.qt.nokia.com/forums/viewthread/10146/ one...[/quote]

    I merged both threads.



  • I have the code exactly as you all have showed me but there was no output. so i put some print statements inside of the loops to see if the compiler was even entering the loops and it is not. What am I doing wrong?

    @int main()
    {
    QStringList stringList;
    QFile textFile("test.txt");
    if (textFile.open(QIODevice::ReadOnly))
    {
    QTextStream textStream(&textFile);
    while (!textStream.atEnd())
    {
    stringList.append(textStream.readLine());
    cout << "test1";
    }
    }
    foreach(const QString line, stringList)
    {
    cout << "test2";
    cout << line.toStdString();
    }
    return 0;
    }@



  • Are you sure that your test.txt are in same directory that your executable file?


  • Moderators

    You should print an error message if it cannot open the file.
    @
    if (...open...)
    {
    ...
    }
    else
    {
    // tell me about it
    }
    @


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.