zoukankan      html  css  js  c++  java
  • file.open异常处理

    当打开一个不存在的文件名,那会导致读文件出现错误,导致程序崩溃。

    为了避免类似问题,我们需要进行判断:

    //open file
    CFile file;
    file.Open("ReadMe.txt", CFile::modeRead,NULL);//open the file

    文件打开返回是文件句柄,所以我们不能判断file对象是否存在而是判断句柄是否存在;

        if ((int)file.m_hFile > 0)
        {
            while (file.Read(szbuf, 200))
            {
                n = 0;
                while (szbuf[n])
                {
                    InforBuf[index++] = szbuf[n++];
                }
                memset(szbuf, 0, 201);
            }
            file.Close();
            InforBuf[index] = 0;
            CString str = InforBuf;
            pt->str = str;
        }
        else
        {
            pt->str = _T("Can't open ReadMe.txt file.");
        }

     file.m_hFile  返回值为0xFFFFFFFF,为异常。

    //open file
        CFile file;
        file.Open("ReadMe.txt", CFile::modeRead,NULL);//open the file
        if ((int)file.m_hFile > 0)
        {
            while (file.Read(szbuf, 200))
            {
                n = 0;
                while (szbuf[n])
                {
                    InforBuf[index++] = szbuf[n++];
                }
                memset(szbuf, 0, 201);
            }
            file.Close();
            InforBuf[index] = 0;
            CString str = InforBuf;
            pt->str = str;
        }
        else
        {
            pt->str = _T("Can't open ReadMe.txt file.");
        }
        ::PostMessage(pt->hwnd, WM_RESPONSE, NULL, (LPARAM)pt);
        Sleep(100);
        return 0;
    View Code

    谢谢.

    End.

  • 相关阅读:
    C#学习
    1.计算机的硬件
    C++ bitset——高端压位卡常题必备STL
    Aragorn's Story
    Sql Server DTS使用
    Django的SQL注意事项(以及时间戳转换日期格式)
    HTML中复选框的使用方法
    Http常见状态码
    scrapy yield 回调函数不执行解决方案
    jsonp跨域请求
  • 原文地址:https://www.cnblogs.com/lumao1122-Milolu/p/13254115.html
Copyright © 2011-2022 走看看