CFile.GetLength()给一个int变量赋值,运行时程序总是意外终止。编程环境vista下vs2005。该如何解决
CFile.GetLength()给一个int变量赋值,运行时程序总是意外终止。编程环境vista下vs2005。
类定义RmpR.h
#pragma once
class BmpR{
public:
BmpR(void);
public:
~BmpR(void);
public:
int nLen;
CString FileName;
void OpenFile();
};
类实现RmpR.cpp
#include "StdAfx.h"
#include "BmpR.h"
using namespace std;
BmpR::BmpR(void){}
BmpR::~BmpR(void){}
void BmpR::OpenFile()
{
CString FileName="lena512.BMP";
CFile file;
if (!file.Open(FileName,file.modeRead)) {cout<<"file open error"<<endl; return;}
nLen=file.GetLength();
}
主文件MFC_test.cpp:
// MFC_test.cpp : Defines the entry point for the console application.//
#include "stdafx.h"
#include "MFC_test.h"
#include "BmpR.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#endif
// The one and only application object
CWinApp theApp;
using namespace std;
int _tmain(int argc, TCHAR* argv[], TCHAR* envp[]){
int nRetCode = 0;
// initialize MFC and print and error on failure
if (!AfxWinInit(::GetModuleHandle(NULL), NULL, ::GetCommandLine(), 0))
{
// TODO: change error code to suit your needs
_tprintf(_T("Fatal Error: MFC initialization failed\n"));
nRetCode = 1;
}
else
{
// TODO: code your application's behavior here.
BmpR *bp=0;
bp->OpenFile(); //调用
}
return nRetCode;
}
是个win32Application下的MFC项目。读一个bmp文件。
编译没任何问题,到cmd窗口下运行时,程序意外终止,能够运行到 nLen=file.GetLength(); 这句话之前。一到这句话,就卡住了。
奇怪的是如果把这句话改成 int nLen2=file.GetLength(); 就没点问题了。就是不能用成员变量 nLen。 真是奇怪,还望大虾们指点一二。
程序出错终止时的问题签名:
问题事件名称: APPCRASH
应用程序名: MFC_test.exe
应用程序版本: 0.0.0.0
应用程序时间戳: 46ffa288
故障模块名称: MFC_test.exe
故障模块版本: 0.0.0.0
故障模块时间戳: 46ffa288
异常代码: c0000005
异常偏移量: 0001a7da
OS 版本: 6.0.6000.2.0.0.768.2
区域设置 ID: 2052
其他信息 1: ac3a
其他信息 2: 1bfb9bbf97582c61ddaecef3add629cc
其他信息 3: 277e
其他信息 4: 8ae5ae80f08bbea955e43d2e032726d4
在线等。
------解决方案--------------------
BmpR *bp=0;
bp- >OpenFile(); //调用
改成这样
BmpR b=0;
b.OpenFile();
或者
BmpR *bp= new BmpR;
bp- >OpenFile();
------解决方案--------------------
BmpR *bp=0;
bp- >OpenFile(); //调用
这个地方显示是错的,需要调用:BmpR * bp = new BmpR();
if (NULL != bp)
{
bp->OpenFile();
}
使用完后一定要设置为:
delete bp;
bp = NULL;
这样才完整,要不然会出现内存泄露,以及野指针的情况。
另外:CString FileName="lena512.BMP";
CFile file;
if (!file.Open(FileName,file.modeRead)) {cout < <"file open error" < <endl; return;}
nLen=file.GetLength();
我在怀疑LZ 的file真的能打开成功吗?
应该使用完整路径加文件名
------解决方案--------------------
这个问题很明显的了,c++的类里的函数,编译之后是作为全局函数来处理,附带传了一个this指针进去。所以只要在函数里面没有使用类成员变量的,完全可以把this 用NULL来处理,所以第二种方法是可以的。第一种方法使用了成员变量nLen所以是不可以的。
类定义RmpR.h
#pragma once
class BmpR{
public:
BmpR(void);
public:
~BmpR(void);
public:
int nLen;
CString FileName;
void OpenFile();
};
类实现RmpR.cpp
#include "StdAfx.h"
#include "BmpR.h"
using namespace std;
BmpR::BmpR(void){}
BmpR::~BmpR(void){}
void BmpR::OpenFile()
{
CString FileName="lena512.BMP";
CFile file;
if (!file.Open(FileName,file.modeRead)) {cout<<"file open error"<<endl; return;}
nLen=file.GetLength();
}
主文件MFC_test.cpp:
// MFC_test.cpp : Defines the entry point for the console application.//
#include "stdafx.h"
#include "MFC_test.h"
#include "BmpR.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#endif
// The one and only application object
CWinApp theApp;
using namespace std;
int _tmain(int argc, TCHAR* argv[], TCHAR* envp[]){
int nRetCode = 0;
// initialize MFC and print and error on failure
if (!AfxWinInit(::GetModuleHandle(NULL), NULL, ::GetCommandLine(), 0))
{
// TODO: change error code to suit your needs
_tprintf(_T("Fatal Error: MFC initialization failed\n"));
nRetCode = 1;
}
else
{
// TODO: code your application's behavior here.
BmpR *bp=0;
bp->OpenFile(); //调用
}
return nRetCode;
}
是个win32Application下的MFC项目。读一个bmp文件。
编译没任何问题,到cmd窗口下运行时,程序意外终止,能够运行到 nLen=file.GetLength(); 这句话之前。一到这句话,就卡住了。
奇怪的是如果把这句话改成 int nLen2=file.GetLength(); 就没点问题了。就是不能用成员变量 nLen。 真是奇怪,还望大虾们指点一二。
程序出错终止时的问题签名:
问题事件名称: APPCRASH
应用程序名: MFC_test.exe
应用程序版本: 0.0.0.0
应用程序时间戳: 46ffa288
故障模块名称: MFC_test.exe
故障模块版本: 0.0.0.0
故障模块时间戳: 46ffa288
异常代码: c0000005
异常偏移量: 0001a7da
OS 版本: 6.0.6000.2.0.0.768.2
区域设置 ID: 2052
其他信息 1: ac3a
其他信息 2: 1bfb9bbf97582c61ddaecef3add629cc
其他信息 3: 277e
其他信息 4: 8ae5ae80f08bbea955e43d2e032726d4
在线等。
------解决方案--------------------
BmpR *bp=0;
bp- >OpenFile(); //调用
改成这样
BmpR b=0;
b.OpenFile();
或者
BmpR *bp= new BmpR;
bp- >OpenFile();
------解决方案--------------------
BmpR *bp=0;
bp- >OpenFile(); //调用
这个地方显示是错的,需要调用:BmpR * bp = new BmpR();
if (NULL != bp)
{
bp->OpenFile();
}
使用完后一定要设置为:
delete bp;
bp = NULL;
这样才完整,要不然会出现内存泄露,以及野指针的情况。
另外:CString FileName="lena512.BMP";
CFile file;
if (!file.Open(FileName,file.modeRead)) {cout < <"file open error" < <endl; return;}
nLen=file.GetLength();
我在怀疑LZ 的file真的能打开成功吗?
应该使用完整路径加文件名
------解决方案--------------------
这个问题很明显的了,c++的类里的函数,编译之后是作为全局函数来处理,附带传了一个this指针进去。所以只要在函数里面没有使用类成员变量的,完全可以把this 用NULL来处理,所以第二种方法是可以的。第一种方法使用了成员变量nLen所以是不可以的。