File Operations

烈酒焚心 提交于 2019-12-09 16:56:37

For example:

    UpdateData(TRUE);
    CFileDialog dlg(1,NULL,NULL,OFN_HIDEREADONLY ,"All Files(*.*)|*.*||");
    if(IDOK!=dlg.DoModal())return;
    m_filename=dlg.GetPathName();	
    UpdateData(FALSE);

When user click browse button, the function UpdateData(TRUE) will refresh the value from controls to variables. As the same reason, the function UpdateData(FALSE) will refresh the value from variables to controls.

#How to open a file with a dialog? The code CFileDialog dlg(1,NULL,NULL,OFN_HIDEREADONLY ,"All Files(*.*)|*.*||"); is supposed to open any files. The following hyperlink contains how to use the CFileDialog member function.

MSDN_CFileDialog::CFileDialog

The fourth parameter dwFlag includes:

    OFN_HIDEREADONLY:隐藏只读选项
    OFN_OVERWRITEPROMPT:覆盖已有文件前提
    OFN_ALLOWMULTISELECT:允许选择多个文件
    OFN_CREATEPROMPT:如果输入的文件名不存在,则对话框返回询问用户是否根据次文件名创建文件的消息框
    OFN_FILEMUSTEXIST:只能输入已存在的文件名
    OFN_FORCESHOWHIDDEN:可以显示隐藏的文件
    OFN_NOREADONLYRETURN:不返回只读文件
    OFN_OVERWRITEPROMPT:保存的文件已存在时,显示文件已存在的信息

Other functions, including DoModel, GetPathName, et al. in CFileDialog:

MSDN_CFileDialog::Other functions

As the begining code shows, now we get the file's path name and we restore it into variabla m_filename, actually, m_filename is a member variable of the correspoding control ID(We usually generate it in MFC ClassWizard).

#How to do file processing? MFC provides the CFile class to edit file. Here we take a short code for example:

    UpdateData(TRUE);
    if(m_filename=="")return;
    CFile ff(m_filename,CFile::modeRead);
    CFile fp(m_savefile,CFile::modeCreate|CFile::modeWrite);
    fp.SeekToBegin();
    ff.SeekToBegin();

    ff.Read(inbuff,sizeof(file));
    /* operations */
    fp.Write(oubuff,sizeof(file));
    ...

At last, after we write the file, we should close it:

    ff.Close();
    fp.Close();

MSDN gives more details about the explaination of CFile class and its member funcitons:

MSDN_CFile::CFile


Tip:

When processing files, we can devide file into small parts and process it respectively. For example, we can take 8 byte from the file everytime:

    /* lFileLen = object.GetLength(); */
    long c=lFileLen/8;
    long d=lFileLen%8;
    for(long i=0;i<c;i++)
    {
        ff.Read(inbuff,8);
        /* operations */
        fp.Write(oubuff,8);
    }
    if(d>0)
    {
        ff.Read(inbuff,d);
        /* operations */
        fp.Write(oubuff,8);
    }
    
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!