Important: Please read the Qt Code of Conduct -

Problems writing a txt file

  • Hi everybody!
    Im having trouble writing a txt file.
    Im supposed to keep a log on my program, and each time a certain "event" happens, im supposed to write it on a txt file, with the date and time.
    The program works, but i dont know why, it adds a couple of strange characters at the beggining of each line.

    This is my code:

    void Grabador::escribir_log(QString data)
    QFile log("log.txt");|QIODevice::WriteOnly);
    QDataStream out(&log);

    ive debugged the program and im sure that the information on data is correct.
    But after i run the program, this is how my log looks like

  • Use QTextStream instead of QDataStream.

  • Thx, that fixed it!

  • You're welcome. QDataStream places some information in front of each item in case it is required to correctly deserialize the item (for example the length of the string as in your case).

Log in to reply