【问题标题】:QTextStream and Visual Studio 2008 release modeQTextStream 和 Visual Studio 2008 发布模式
【发布时间】:2010-12-05 21:31:39
【问题描述】:

我有一个使用 QTextStream 的简单代码,它在 Visual Studio 中的调试模式下工作得非常好,但是如果我把它放在发布模式下它不会t read anything from the file. I included QtCore4.lib for the release mode and for the debug mode QtCored4.lib. Im 使用 Qt4.6.3 vs2008,如果它工作可能会出现什么问题在调试模式? 我在下面插入代码:

#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);
 const char *liner;
    while(!(line = stream.readLine()).isNull()) 
      if (!line.isEmpty()) {
    baline = line.toLatin1();
    liner = baline.data();
        cout << liner << endl;
    }

【问题讨论】:

    标签: c++ visual-studio-2008 qt release


    【解决方案1】:

    那是因为您将带有副作用的代码放入了断言中:

    assert(qfile.open( QIODevice::ReadOnly | QIODevice::Text));
    

    此代码永远不会在发布模式下执行。不仅断言被禁用,而且其中的代码也不会被执行!规则:永远不要在 assert() 中放入任何有副作用的东西。当某些东西在调试模式下工作但在发布模式下不工作时,这是首先要寻找的东西。

    如果你想断言,这样做:

    const bool opened = qfile.open( QIODevice::ReadOnly | QIODevice::Text);
    assert( opened );
    

    【讨论】:

    • 每个人都至少这样做一次。断言很有用,但也有点邪恶。
    • 是的,永远不要在应用程序控制之外的条件下使用 assert() - 例如文件系统状态、输入数据、用户操作等。但是这个问题当然也可能发生在完全有效的 assert() 用法中.
    • 感谢您的帮助!这解决了我的问题,我不知道在发布模式下会跳过断言。
    猜你喜欢
    • 2015-09-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-09-02
    • 2010-12-27
    相关资源
    最近更新 更多