Solved Why won't my program write to a file?
-
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?
-
if (debugMode) { }
Are you sure that block is being executed, i.e.
debugMode
equalstrue
/1
? -
@Diracsbracket yes
-
@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 aQDataStream
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:
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 an8
, 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 usedQTextStream
instead.