Problem writing out to a stream
-
Hi,
I'm trying to write out to a file using QTextStream and although it compiles and runs, nothing goes to the file. Can't seem to figure what I'm doing wrong. Here is a simplified version. The file does get created in the directory just nothing writes to it.
static QTextStream outStream; //keep the handle since we can go into the method multiple times
static bool once = false;if(once == false)
{
once = true;
QDir someDir(logDir); //logDir = some director c:"\blah\blah
if(someDir.exists())
{
QString fileName = logDir + "/" + "myfileName";
QFile file(filename);
if (file.open(QIODevice::WriteOnly | QIODevice::Append | QIODevice::Text))
{
outStream.setDevice(&file);
}}
}
Qstring someText = "blah blah";
outStream << someText << "\n";Any help would be appreciated.
-
I tried closing the file and sure enough the data was logged to the file, so I tried using file.flush() and nothing was written to the file. Any ideas how to force the data to the file?
-
Hi,
I'm trying to write out to a file using QTextStream and although it compiles and runs, nothing goes to the file. Can't seem to figure what I'm doing wrong. Here is a simplified version. The file does get created in the directory just nothing writes to it.
static QTextStream outStream; //keep the handle since we can go into the method multiple times
static bool once = false;if(once == false)
{
once = true;
QDir someDir(logDir); //logDir = some director c:"\blah\blah
if(someDir.exists())
{
QString fileName = logDir + "/" + "myfileName";
QFile file(filename);
if (file.open(QIODevice::WriteOnly | QIODevice::Append | QIODevice::Text))
{
outStream.setDevice(&file);
}}
}
Qstring someText = "blah blah";
outStream << someText << "\n";Any help would be appreciated.
-
Hi,
I'm trying to write out to a file using QTextStream and although it compiles and runs, nothing goes to the file. Can't seem to figure what I'm doing wrong. Here is a simplified version. The file does get created in the directory just nothing writes to it.
static QTextStream outStream; //keep the handle since we can go into the method multiple times
static bool once = false;if(once == false)
{
once = true;
QDir someDir(logDir); //logDir = some director c:"\blah\blah
if(someDir.exists())
{
QString fileName = logDir + "/" + "myfileName";
QFile file(filename);
if (file.open(QIODevice::WriteOnly | QIODevice::Append | QIODevice::Text))
{
outStream.setDevice(&file);
}}
}
Qstring someText = "blah blah";
outStream << someText << "\n";Any help would be appreciated.
@leinad said in Problem writing out to a stream:
This code cannot work!!
static QTextStream outStream; //keep the handle since we can go into the method multiple timesWhy
static??? please use class member for this!static bool once = false;What this and not use
outStream.device() != nullptrto check if stream has been initialized?if(once == false) { once = true; QDir someDir(logDir); //logDir = some director c:"\blah\blah if(someDir.exists()) { QString fileName = logDir + "/" + "myfileName"; QFile file(filename); if (file.open(QIODevice::WriteOnly | QIODevice::Append | QIODevice::Text)) { outStream.setDevice(&file); } }==> After here
fileis destroyed and closed and outStream use an invalid QIODevice!} Qstring someText = "blah blah"; outStream << someText << "\n";