[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

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM