QTextStream and Visual Studio 2008 release mode
I have a simple code using QTextStream and it works very fine in visual studio in debug mode, but if I put it in release mode it doesnt read anything from the file. I included QtCore4.lib for the release mode and for the debug mode QtCored4.lib. I
m using Qt4.6.3 vs2008,what could be the problem if it works in debug mode?
I insert the code below:
#include <iterator>
#include <QFile>
#include <QTextStream>
#include <QString>
#include<iostream>
#include<fstream>
#include<iterator>
#include<assert.h>
#include<stdio.h>
using namespace std;
void main()
{
QString qsArgsFile = "curexp.txt",line;
QByteArray baline;
cout<<qsArgsFile.toAscii().data();
QFile qfile( qsArgsFile );
assert(qfile.open( QIODevice::ReadOnly | QIODevice::Text));
QTextStream stream( &qfile );
baline = qfile.read(50)开发者_JS百科;
const char *liner;
while(!(line = stream.readLine()).isNull())
if (!line.isEmpty()) {
baline = line.toLatin1();
liner = baline.data();
cout << liner << endl;
}
That's because you put code with side-effects into an assert:
assert(qfile.open( QIODevice::ReadOnly | QIODevice::Text));
This code is never executed in release mode. Not only are assertions disabled, also the code inside them is not executed! Rule: Never put anything with side-effects inside an assert(). This is the first thing to look for when something works in debug mode but not in release mode.
If you want to assert, do it like this:
const bool opened = qfile.open( QIODevice::ReadOnly | QIODevice::Text);
assert( opened );
精彩评论