⚠️ Forum Maintenance: Feb 6th, 8am - 14pm (UTC+2)

Read line and store info?

  • Hello, so I'm fiddling around with qfile and looked at some tutorials, but I can't see how I can read the file line by line to for example
    store line 1 in username
    and line 2 as password.

    Currently doing this:
    QTextStream out(&file);
    out << username << endl;
    out << password << endl;

    It outputs the details in the format:

    I want to now store test in username and the other test in password, not sure if getline is what im looking for.

  • I tried doing something like
    QString line[2] = out.readLine();
    ui->lineusername->text() = line[0];
    ui->linepassword->text() = line[1];

    an array cannot be initialized with a parenthesized initializer

  • out.device()->seek(0); // move back to the start of the file

  • @davethedave
    In addition to what @VRonin has shown, if your lineusername/linepassword are, say, QLineEdits, you cannot assign via text() = ... as you tried in yours, you'll want to use setText(). [EDIT As @VRonin 's example now shows.]

Log in to reply