Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Why won't my program write to a file?



  • 0_1525494089136_0d9b76b4-72ea-4d07-89df-7749a35118a9-image.png

    0_1525494134485_a5cb4349-4f98-4f76-824d-13832064064c-image.png

    0_1525494162706_9c4cd305-b0da-4b49-9685-26171d731c56-image.png

    At the end of a normal program run in debugMode nothing happens, the file remains empty. The same thing happened before. Is it a directory issue?



  • @Hubbard

    if (debugMode)
    {
    }
    

    Are you sure that block is being executed, i.e. debugMode equals true/1 ?





  • @Hubbard
    Sorry for the obvious questions. I hope you are not taking offence...
    Are your sure you are looking at the correct output dir when you are looking for the file?
    Is the file being created even when you write nothing to it? (i.e. just open/close it?)

    P.S.
    When writing strings to a QDataStream I usually get additional strange characters if I just
    use the << stream operator. Instead, I have to use:

    outputStream->writeRawData("test", 4);
    

    For example, if I use

    *outputStream << QString("test");
    

    or

    *outputStream << "test";
    

    I get the following file:

    0_1525497934610_6049225e-ef69-4c8a-94d9-2fe209e6f9b7-image.png

    While using writeRawData() gives:

    0_1525497986026_d976d712-d74e-46f4-982c-ddf3fca80225-image.png



  • The file is not so I guess I am looking at the wrong directory, where does Qt like to create files to??



  • Aah I see, it creates the log to the build file, that makes sense, thank you!



  • The streams do seem to be causing issues, they won't show the ints, just a box



  • @Hubbard said in Why won't my program write to a file?:

    The streams do seem to be causing issues, they won't show the ints, just a box

    Don't forget you are writing binary data (since that is what QDataStream is for)! So when you open it in a text editor, those numbers will usually not correspond to valid displayable characters (e.g. ascii). If you write an 8, it will write the binary 8, not the ascii character 8.

    If you want those numbers as ascii, then you need to use the QString::number() conversion.
    For plain text, you could otherwise also have used QTextStream instead.


Log in to reply