Why won't my program write to a file?

  • 0_1525494089136_0d9b76b4-72ea-4d07-89df-7749a35118a9-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?)

    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");


    *outputStream << "test";

    I get the following file:


    While using writeRawData() gives:


  • 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