[MFC] CFile读写文件实现(高效)

1.文件写入

void  CMFCApplication1Dlg::Write()
{
    CFile file;
    CString FileName = "D:\100w.txt";
    memset(buf, 0, NUM);//初始化内存,防止读出字符末尾出现乱码
    for (int i = 0; i < NUM; i++)
    {
        if ((i / 2000)%2 == 0)
            buf[i] =10000;
        else
            buf[i] = 0;
    } 
    // TODO: Add your control notification handler code here
    int aa = buf[NUM/2];
    try
    {
        file.Open(FileName, CFile::modeCreate | CFile::modeWrite|CFile::typeBinary);
        //CArchive ar(&file, CArchive::store);//根据打开的文件,创建文件串行化对象
        //Serialize(ar); //写文件内容
        //ar.Write(buf, NUM);
        ////结束后关闭对象
        //ar.Close();
        file.SeekToBegin();
        int len = NUM*sizeof(short int);
        file.Write(buf, NUM*sizeof(short int));//CString m_data
        //file.Flush();
        file.Close();
        MessageBox("写入成功!");
    }
    catch (CFileException *e)
    {
        CString str;
        str.Format("写入失败的原因是:%d", e->m_cause);
        MessageBox("str");
        file.Abort();
        e->Delete();
    }
}

2.文件读取

void CMFCApplication1Dlg::Read()
{
    CFile file;
    CString FileName = "D:\data.txt";
    short int buf[100];//读1K
    memset(buf, 0, 100*sizeof(short int));//初始化内存,防止读出字符末尾出现乱码
    try
    {
        if (!file.Open(FileName, CFile::modeRead))
        {
            MessageBox("没有文件!");
            return;
        }
        file.Seek(10000 * sizeof(short int), CFile::begin);
        
        //结构体格式读取
        //DATAS aa;
        //file.Read(&aa, sizeof(aa));
        file.Read(buf, sizeof(buf));
        file.Close();
        MessageBox("读出成功!");
    }
    catch (CFileException *e)
    {
        CString str;
        str.Format("读取数据失败的原因是:%d", e->m_cause);
        MessageBox("str");
        file.Abort();
        e->Delete();
    }
}                                    

3.文件的查找

  当对一个文件操作时,如果不知道该文件是否存在,就要首先进行查找。MFC中有一个专门用来进行文件查找的类CFileFind,使用它可以方便快捷地进行文件的查找。下面这段代码演示了这个类的最基本使用方法。

  CString strFileTitle;
  CFileFind finder;
  BOOL bWorking = finder.FindFile("C:\windows\sysbkup\*.cab");

4.项目用到的分页读取

读:

void  CWave::ShowByPaging_W(CString strPath, int nPageNum, bool bIsShowI, bool bIsShowQ)
{
    CFile file;
    int nReadDataCount = 0;
    memset(data_W, 0, nDataNumOfPage_W*2 * sizeof(short int));
    try
    {
        if (!file.Open(strPath, CFile::modeRead))
        {
            MessageBox("没有文件!");
            return;
        }
        file.Seek((nPageNum-1) *nDataNumOfPage_W*2*sizeof(short int), CFile::begin);
        
        //返回实际读到参数。若读出数据小于20W,则表示已到末尾;此时读的是字节数
        int nReadChar = file.Read(data_W, sizeof(short int)*2*nDataNumOfPage_W);
        nReadDataCount = nReadChar / 2 / sizeof(short int);
        //file.Read(buf, sizeof(buf));
        file.Close();
    }
    catch (CFileException *e)
    {
        //CString str;
        //str.Format("读取数据失败的原因是:%d", e->m_cause);
        //MessageBox("str");
        file.Abort();
        e->Delete();
    }
    int startLoc = nDataNumOfPage_W*(m_nCurPageNum_W-1);
    int count = 0;
    for (int i = 0; i <nDataNumOfPage_W * 2; i++)
    {
        if (i % 2 == 1)
            data_WQ[i / 2] = data_W[i] * m_DeltaY_W;
        else 
            data_WI[i / 2] = data_W[i] * m_DeltaY_W;
        if (i % 2 == 0)
        {
            count++;
            data_WX[i / 2] = (startLoc+count)* m_DeltaX_W;
        }    
    }

    LoadData_W(nDataNumOfPage_W, data_WX, data_WI, data_WQ, bIsShowI, bIsShowQ);
}

写:

bool writeFileByData(CString strFileName, UINT16 buf[], int len)
{
    string strPath = m_strSavePath_trans+"\" + strFileName + ".bin";

    CFile file;
    try
    {
        //CFile::modeCreate  创建方式打开文件,如文件已存在则将其长度设置为0
        //CFile::modeNoTruncate 创建文件时如文件已存在不对其进行截断
        //CFile::modeRead 只读方式打开文件
        //CFile::modeReadWrite 读写方式打开文件
        //CFile::modeWrite 写入方式打开文件
        //CFile::typeBinary 设置文件为二进制模式
        //CFile::typeText 设置文件为文本模式
        file.Open(strPath.c_str(), CFile::modeCreate|CFile::modeNoTruncate | CFile::modeWrite | CFile::typeBinary);
        //CArchive ar(&file, CArchive::store);//根据打开的文件,创建文件串行化对象
        //Serialize(ar); //写文件内容
        //ar.Write(buf, NUM);
        ////结束后关闭对象
        //ar.Close();
        file.SeekToEnd();
        file.Write(buf, len*sizeof(UINT16));//CString m_data
        //file.Flush();
        file.Close();
        return true;
    }
    catch (CFileException *e)
    {
        CString str;
        str.Format("写入失败的原因是:%d", e->m_cause);
        //MessageBox("str");
        file.Abort();
        e->Delete();
        return false;
    }
    return false;
}

1. 文件模式标志 说明

  CFile::modeCreate  创建方式打开文件,如文件已存在则将其长度设置为0

  CFile::modeNoInherit  不允许继承

  CFile::modeNoTruncate 创建文件时如文件已存在不对其进行截断

  CFile::modeRead 只读方式打开文件

  CFile::modeReadWrite 读写方式打开文件

  CFile::modeWrite 写入方式打开文件

  CFile::shareCompat 在使用过程中允许其他进程同时打开文件

  CFile::shareDenyNone 在使用过程中允许其他进程对文件进行读写

  CFile::shareDenyRead 在使用过程中不允许其他进程对文件进行读取

  CFile::shareDenyWrite 在使用过程中不允许其他进程对文件进行写入

  CFile::shareExclusive  取消对其他进程的所有访问

  CFile::typeBinary 设置文件为二进制模式

  CFile::typeText 设置文件为文本模式

参考链接:

http://www.jizhuomi.com/software/234.html

http://www.jizhuomi.com/software/497.html

http://blog.sina.com.cn/s/blog_69b9bb050100lelo.html

http://blog.csdn.net/iscassucess/article/details/8210069

原文地址:https://www.cnblogs.com/zwh0214/p/6292036.html