Home > Software engineering >  QT C how to append text on last lıne and keep old lınes?
QT C how to append text on last lıne and keep old lınes?

Time:07-27

Instead of overwriting the new text, I want to preserve the existing content and add it to the new line.

I need make a simple log for login. But When I try save to txt file Is overwriting on file. But when I try it, It just overwriting on file. How can I add to new line when new log is coming?

Here is my login function

void loginpage::on_lgnBtn_clicked() {
    login = ui -> lgnUname -> text();
    password = ui -> lgnPass -> text();
    QString passwordhash = passwordHash(password.toUtf8());
    qDebug() << passwordhash;
    if (login == "admin" && passwordhash == getTxtPassword()) {
        loginLog();
        hide();
        mw = new MainWindow(this);
        mw -> show();
    } else {
        QMessageBox::warning(nullptr, "Hata", "Kullanıcı adınız veya şifreniz hatalı!");
        loginLog();
    }
}

Here is my function for write to file.

void loginpage::loginLog() {

    QString TEXT_DIR = QStandardPaths::writableLocation(QStandardPaths::DocumentsLocation)   "/test/log.txt";

    QFile file(TEXT_DIR);
    if (file.open(QIODevice::WriteOnly | QIODevice::Text)) {
        QTextStream stream( & file);
        QString UnameLog = login;
        QString PassLog = password;

        stream << "Giriş yapıldı";
        stream << "\n";
        stream << UnameLog;
        stream << "\n";
        qDebug()<< UnameLog;
        stream << PassLog;

        file.close();
    } else {
        qDebug() << "hata log yazılamadı";
    }
}

I think there is something missing or wrong, but I couldn't figure it out. Can you help me please?

CodePudding user response:

Did you check the documentation? The flag you want is QIODevice::Append.

  • Related