2010-12-05 4 views
1

QTextStream을 사용하는 간단한 코드가 있으며 디버그 모드에서 매우 잘 작동하지만 출시 모드로 설정하면 QT4.6.3 vs2008을 사용하여 t read anything from the file. I included QtCore4.lib for the release mode and for the debug mode QtCored4.lib. Im을 사용할 수 있습니다. 문제가 디버그 모드에서 작동하는 경우? 나는 아래 코드 삽입 :QTextStream 및 Visual Studio 2008 릴리스 모드

#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; 
    } 

답변

2

당신이 어설으로 부작용과 코드를 삽입하기 때문이다 :

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

이 코드는 릴리스 모드에서 실행되지 않습니다를. 어설 션이 비활성화되어있을뿐만 아니라 내부의 코드가 실행되지 않습니다! 규칙 : 부작용이있는 항목을 어설 션() 안에 넣지 마십시오. 이것은 무엇인가가 디버그 모드에서는 작동하지만 릴리즈 모드에서는 작동하지 않을 때 제일 먼저 찾아야합니다. 당신이 주장 할 경우

, 이런 식으로 작업을 수행합니다

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

모두가 적어도 한 번이한다. 주장은 유용하지만 약간 악합니다. – Thomi

+0

그렇습니다. 파일 시스템 상태, 입력 데이터, 사용자 동작 등과 같이 응용 프로그램 제어 이외의 조건에서는 절대로 assert()를 수행하지 마십시오. 물론이 문제는 assert()의 완전히 유효한 사용에도 발생할 수 있습니다. –

+0

도움 주셔서 감사합니다! 이게 내 문제를 해결했고, 릴리스 모드에서는 assert가 건너 뛴다는 것을 알지 못했습니다. – Corgan