CFileException

CFileException 异常类的一种,用于检测文件操作中可能出现的错误。CFileException 异常类的一种,用于检测文件操作中可能出现的错误。

CFileException类的声明文件保存在头文件afx.h中。

当我们在使用CFile及其派生类的对象的时候,如果产生异常则会创建和抛出CFileException对象。采用TRY…CATCH…END_CATCH。

一个CFileException对象代表了与文件相关的异常。CFileException类包括有可移植的原因代码和操作系统指定错误值等公共数据成员。此类也提供产生文件异常的静态成员函数,返回操作系统和C运行中的错误原因代码。

CFileException对象在CFile成员函数中被创建和产生,也可在派生类成员函数中创建或产生。可在CATCH表达式范围内访问这些对象。因为可移植,可以仅用出错代码获取异常的原因。要了解关于异常的更多信息,可参阅联机文档“Visual C++程序员指南”中的“异常”。

CFileException类的成员

数据成员 m_cause 包含异常原因相对应的可移植代码

m_IosError 包含相关操作系统错误代码

m_strFileName 包含异常的文件名

构造函数 CFileException 构造一个CFileException对象

代码转换 OsErrorToException 返回与操作系统错误值相对应的出错原因代码

ErrnoToException 返回与运行错误值相对应的出错原因代码

帮助函数 ThrowOsError 返回一个基于操作系统错误值相对应的出错原因代码

ThrowErrno 产生一个基于运行错误值的文件异常

CFileException::CFileException

CFileException(int cause = CFileException::none,LONG lOsError = -1);

参数: cause 一个枚举类型变量,标明异常原因。可参阅CFileException::m_cause,了解可能值的列表。

lOsError 可能的话,是异常的操作系统指定原因。lOsError参数提供比cause更多的信息。

说明:

构造一个CFileException对象,存储出错代码和对象中的操作系统代码。

不要直接使用此构造函数,而应调用全局函数AfxThrowFileException

注意:

变量lOsError仅可应用于CFileCStdioFile对象,CMemFile类不处理此错误代码。

/*SUNIX??ì¨?′?? v1.4*/ // SelectVectorDlg.cpp : implementation file // #include "stdafx.h" #include "sp_draw.h" #include "SelectVectorDlg.h" #ifdef _DEBUG #define new DEBUG_NEW #undef THIS_FILE static char THIS_FILE[] = __FILE__; #endif ///////////////////////////////////////////////////////////////////////////// // CVectorStatic CVectorStatic::CVectorStatic() { } CVectorStatic::~CVectorStatic() { } BEGIN_MESSAGE_MAP(CVectorStatic, CStatic) //{{AFX_MSG_MAP(CVectorStatic) ON_WM_PAINT() //}}AFX_MSG_MAP END_MESSAGE_MAP() ///////////////////////////////////////////////////////////////////////////// // CVectorStatic message handlers void CVectorStatic::OnPaint() { CPaintDC dc(this); // device context for painting // TODO: Add your message handler code here CRect rt1; GetClientRect(&rt1); CBrush brush; brush.CreateSolidBrush(RGB(255,255,255)); CBrush* pOldBrush=dc.SelectObject(&brush); CPen pen; pen.CreatePen(PS_SOLID,1,RGB(192,192,192)); CPen *poldpen=dc.SelectObject(&pen); dc.Rectangle(&rt1); dc.SelectObject(pOldBrush); dc.SelectObject(poldpen); CSize sz=m_vector.GetSize(); if ((sz.cx==0)||(sz.cy==0)) return; float scaleX=1; float scaleY=1; if((rt1.Width()<sz.cx)||(rt1.Height()<sz.cy)) { if (rt1.Width()<sz.cx) scaleX=((float)rt1.Width())/(float)(sz.cx+1); if (rt1.Height()<sz.cy) scaleY=((float)rt1.Height())/(float)(sz.cy+1); float scale=__max(scaleX,scaleY); sz.cx=__min(rt1.Width()-10,(int)((float)(sz.cx-1)*scale)); sz.cy=__min(rt1.Height()-10,(int)((float)(sz.cy-1)*scale)); } else { if (rt1.Width()>sz.cx) scaleX=(int)((float)rt1.Width())/(float)(sz.cx+1); if (rt1.Height()>sz.cy) scaleY=(int)((float)rt1.Height())/(float)(sz.cy+1); float scale=__min(scaleX,scaleY); sz.cx=__min(rt1.Width()-10,(int)((float)(sz.cx-1)*scale)); sz.cy=__min(rt1.Height()-10,(int)((float)(sz.cy-1)*scale)); } rt1.SetRect(5,5,sz.cx,sz.cy); m_vector.Draw(&dc,rt1,rt1,0,FALSE); // Do not call CStatic::OnPaint() for painting messages } void CVectorStatic::SetVectFile(LPCSTR filepath) { m_vector.Load(filepath); Invalidate(TRUE); } ///////////////////////////////////////////////////////////////////////////// // CSelectVectorDlg property page IMPLEMENT_DYNCREATE(CSelectVectorDlg, CPropertyPage) CSelectVectorDlg::CSelectVectorDlg() : CPropertyPage(CSelectVectorDlg::IDD) { //{{AFX_DATA_INIT(CSelectVectorDlg) m_bFill = FALSE; m_vector = _T(""); //}}AFX_DATA_INIT } CSelectVectorDlg::~CSelectVectorDlg() { } void CSelectVectorDlg::DoDataExchange(CDataExchange* pDX) { CPropertyPage::DoDataExchange(pDX); //{{AFX_DATA_MAP(CSelectVectorDlg) DDX_Control(pDX, IDC_LIST1, m_list); DDX_Check(pDX, IDC_FILL, m_bFill); DDX_Text(pDX, IDC_EDIT1, m_vector); DDV_MaxChars(pDX, m_vector, 32); //}}AFX_DATA_MAP DDX_Control(pDX, IDC_TAB1, m_TabCtrl); } BEGIN_MESSAGE_MAP(CSelectVectorDlg, CPropertyPage) //{{AFX_MSG_MAP(CSelectVectorDlg) ON_LBN_SELCHANGE(IDC_LIST1, OnSelchangeList1) ON_BN_CLICKED(IDC_COLOR, OnColor) //}}AFX_MSG_MAP ON_NOTIFY(TCN_SELCHANGE, IDC_TAB1, &CSelectVectorDlg::OnTcnSelchangeTab1) END_MESSAGE_MAP() ///////////////////////////////////////////////////////////////////////////// // CSelectVectorDlg message handlers BOOL CSelectVectorDlg::OnInitDialog() { CPropertyPage::OnInitDialog(); // TODO: Add extra initialization here m_ctrl_ToColor.SubclassDlgItem(IDC_COLOR,this); m_ctrlStatic.SubclassDlgItem(IDC_VECTOR,this); CString str; CStdioFile file; BOOL res = true; if (file.Open("C://sunpac//par//global//vec//vec.ini",CFile::modeRead|CFile::typeText)==FALSE) { res = false; } if(res) { res = ReadVecType(&file); } if(!res) { FillBox(); } else { int i = m_TabCtrl.GetCurSel(); TCITEM tcItem; char buffer[64]={0}; tcItem.pszText= buffer; tcItem.cchTextMax=32; tcItem.mask= TCIF_TEXT; m_TabCtrl.GetItem(i,&tcItem); CString type = buffer; FillBox(&file,type); } if(res) { file.Close(); } if(!m_vector.IsEmpty()) m_list.SelectString(0,m_vector); OnSelchangeList1(); m_ctrl_ToColor.m_Color=m_ToColor; m_ctrl_ToColor.Invalidate(); return TRUE; // return TRUE unless you set the focus to a control // EXCEPTION: OCX Property Pages should return FALSE } BOOL CSelectVectorDlg::ReadVecType(CStdioFile *pfile) { CString str; BOOL bl1,bl2; DWORD dwflag = 0; char strName[33]; int iret; int index = 0; //移动读指针到画面段落 if (SeekSection(pfile,"type")==FALSE) return FALSE; try { //读取下一行字符串 bl1=pfile->ReadString(str); if (!bl1) return FALSE; str.TrimLeft(); str.TrimRight(); //判断字符串是否是段落名 bl2=HitTestSectionID(str); if (bl2) return FALSE; while ((!bl2)&&(bl1)) { //读入画面名称和画面标志 iret=sscanf(str,"%x=%[^;]33s",&dwflag,strName);//%[^=]s if (iret==2) { m_TabCtrl.InsertItem(index, strName); index++; } //读取下一行字符串 bl1=pfile->ReadString(str); if (!bl1) break; str.TrimLeft(); str.TrimRight(); //判断字符串是否是段落名 bl2=HitTestSectionID(str); if (bl2) break; } return TRUE; } catch(CFileException* e) { e->Delete(); return FALSE; } } BOOL CSelectVectorDlg::SeekSection(CStdioFile *pfile, LPCSTR strSection) { CString str; BOOL bret; assert(strSection!=NULL); assert(pfile!=NULL); try { pfile->SeekToBegin(); bret=pfile->ReadString(str); while (bret) { if (HitTestSectionID(str)==TRUE) { if ((HitTestSectionName(str,strSection))==TRUE) return TRUE; } bret=pfile->ReadString(str); } return FALSE; } catch(CFileException* e) { e->Delete(); return FALSE; } } BOOL CSelectVectorDlg::HitTestSectionID(LPCSTR str) { CString s; assert(str!=NULL); s=str; s.TrimLeft(); if (s.IsEmpty()) return FALSE; if ((s[0]=='[')&&(s.FindOneOf("]")>0)) return TRUE; return FALSE; } BOOL CSelectVectorDlg::HitTestSectionName(LPCSTR strSection, LPCSTR strName) { assert(strSection!=NULL); assert(strName!=NULL); CString str="["; str=str+strName+"]"; if (_strnicmp(strSection,str,str.GetLength())==0) return TRUE; else return FALSE; } void CSelectVectorDlg::SetContent(LPCSTR vectorname, BOOL bFill, COLORREF color) { m_ToColor=color; m_bFill=bFill; m_vector=vectorname; } void CSelectVectorDlg::GetContent(CString &vectorname, BOOL &bFill, COLORREF &color) { color=m_ToColor; bFill=m_bFill; vectorname=m_vector; } void CSelectVectorDlg::FillBox() { CFileFind vectordir; CString file=theApp.GetDirectory(); CString vectorname; file+="global\\vec\\*.vec"; m_list.ResetContent(); if (vectordir.FindFile(file)) { while (vectordir.FindNextFile()) { vectorname=vectordir.GetFileTitle(); m_list.AddString(vectorname); } vectorname=vectordir.GetFileTitle(); m_list.AddString(vectorname); vectordir.Close(); } } void CSelectVectorDlg::FillBox(CStdioFile *pfile,CString type) { m_list.ResetContent(); CString str; BOOL bl1,bl2; DWORD dwflag = 0; char strName[33]; int iret; //移动读指针到画面段落 if (SeekSection(pfile,type)==FALSE) return; try { //读取下一行字符串 bl1=pfile->ReadString(str); if (!bl1) return; str.TrimLeft(); str.TrimRight(); //判断字符串是否是段落名 bl2=HitTestSectionID(str); if (bl2) return; while ((!bl2)&&(bl1)) { //读入画面名称和画面标志 iret=sscanf(str,"%x=%[^;]33s",&dwflag,strName);//%[^=]s if (iret==2) { m_list.AddString(strName); } //读取下一行字符串 bl1=pfile->ReadString(str); if (!bl1) break; str.TrimLeft(); str.TrimRight(); //判断字符串是否是段落名 bl2=HitTestSectionID(str); if (bl2) break; } return; } catch(CFileException* e) { e->Delete(); return; } } void CSelectVectorDlg::OnSelchangeList1() { // TODO: Add your control notification handler code here int pos=m_list.GetCurSel(); CString vectorname; if (pos>=0) { m_list.GetText(pos,vectorname); m_vector=vectorname; CString file; file.Format("%sglobal\\vec\\%s.vec",theApp.GetDirectory(),vectorname); m_ctrlStatic.SetVectFile(file); } UpdateData(FALSE); } void CSelectVectorDlg::OnColor() { // TODO: Add your control notification handler code here if (m_ToColor==m_ctrl_ToColor.m_Color) return; m_ToColor=m_ctrl_ToColor.m_Color; } void CSelectVectorDlg::OnTcnSelchangeTab1(NMHDR *pNMHDR, LRESULT *pResult) { int i = m_TabCtrl.GetCurSel(); TCITEM tcItem; char buffer[64]={0}; tcItem.pszText= buffer; tcItem.cchTextMax=32; tcItem.mask= TCIF_TEXT; m_TabCtrl.GetItem(i,&tcItem); CStdioFile file; if (file.Open("C://sunpac//par//global//vec//vec.ini",CFile::modeRead|CFile::typeText)==TRUE) { FillBox(&file,buffer); if(!m_vector.IsEmpty()) m_list.SelectString(0,m_vector); OnSelchangeList1(); file.Close(); } *pResult = 0; }
07-18
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值