如何创建文件usinf cfiledialog [英] How to cretae file usinf cfiledialog

查看:107
本文介绍了如何创建文件usinf cfiledialog的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用vc ++ mfc创建文件但是它的创建文件夹而不是文件



我尝试过:



i want to create file using vc++ mfc but its creating folder instead of file

What I have tried:

CString fileName;
    wchar_t* p = fileName.GetBuffer(FILE_LIST_BUFFER_SIZE);
    CFileDialog dlgFile(TRUE);
    OPENFILENAME& ofn = dlgFile.GetOFN();
    ofn.lpstrFilter = L"XML\0*.xml\0All\0*.*\0";
    ofn.Flags |= OFN_CREATEPROMPT;
    ofn.lpstrFile = p;
    ofn.nMaxFile = FILE_LIST_BUFFER_SIZE;

    if (dlgFile.DoModal() == IDOK)
    {
            fileName //its creating foldername

    }




could you please correct me if im doing wrong here



可以


could

推荐答案

CFileDialog 不会创建文件或目录。这是一个用于选择打开或保存文件的对话框,并返回所选名称。如果该文件尚不存在,则由该路径名创建该文件。



也无需在<$ c中传递缓冲区$ c> OPENFILENAME 结构,因为不允许多项选择。



然后只需使用 CFileDialog GetPathName()用于检索全名的成员函数:

The CFileDialog will not create files or directories. It is a dialog to select files for opening or saving and returns the selected names. It is up to you to create the file from that path name if it does not exist yet.

There is also no need to pass a buffer in the OPENFILENAME structure because when not allowing multiple selections.

Then just use the CFileDialog GetPathName() member function to retrieve the full name:
CFileDialog dlgFileDlg (TRUE, "xml", fileName.GetString(),
      OFN_CREATEPROMPT, L"XML\0*.xml\0All\0*.*\0", this);
if ( dlgFile.DoModal () == IDOK )
{
    CString m_strPathname = dlgFile.GetPathName();
}





使用 OFN_ALLOWMULTISELECT 时将缓冲区设置为你的代码。然后返回的字符串包含 NULL 终止的目录名,后跟 NULL 终止的文件名最终被另一个<$ c终止$ c> NULL 字符。也许这就是你所观察到的。



但你仍然可以使用 CFileDialog 成员来获取循环:



When using OFN_ALLOWMULTISELECT set the buffer as done in your code. Then the returned string contains the NULL terminated directory name followed by also NULL terminated file names finally terminated by another NULL character. Maybe that is what you have observed.

But you can still use CFileDialog members to get the names within a loop:

if ( dlgFile.DoModal () == IDOK )
{
    POSITION pos = dlgFile.GetStartPosition();
    while (pos)
    {
        CString m_strPathname = dlgFile.GetNextPathName(pos);
    }
}


这篇关于如何创建文件usinf cfiledialog的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆