File Operations

本文介绍了如何使用CFileDialog进行文件对话框的操作及如何利用CFile进行文件读写处理。具体包括打开文件对话框的参数设置、常用成员函数说明、文件读写的流程展示等。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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);
    }
    

转载于:https://my.oschina.net/1212sd/blog/626569

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值