Click here to Skip to main content
15,894,337 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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
Posted
Updated 7-May-18 1:06am

1 solution

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


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);
    }
}
 
Share this answer
 
Comments
Member 13089825 7-May-18 7:33am    
i want to replace the BROWSEINFO with Cfiledialog
CString fileName;
BROWSEINFO bi;
ZeroMemory(&bi, sizeof(bi));
TCHAR szDisplayName[MAX_PATH] = { 0 };
bi.hwndOwner = GetSafeHwnd();
bi.pidlRoot = NULL;
bi.pszDisplayName = szDisplayName;
bi.lpszTitle = _T("Please createfile :");
bi.ulFlags = BIF_RETURNONLYFSDIRS;
bi.lParam = NULL;
bi.iImage = 0;
LPITEMIDLIST pidl = SHBrowseForFolder(&bi);
TCHAR szPathName[MAX_PATH] = { 0 };
if (NULL != pidl)
{
BOOL bRet = SHGetPathFromIDList(pidl, szPathName);
if (FALSE == bRet)
return;
}
//On cancel button click,directory name is not changed
if (szPathName[0] != '\0')
{
filename
}
Jochen Arndt 7-May-18 7:42am    
"i want to replace the BROWSEINFO with Cfiledialog" is contrary to your question "i want to create file using vc++ mfc but its creating folder instead of file".

If you really want folder selection:
SHBrowseForFolder is - as the name suggests - for selecting folders (directories) while CFileDialog is for selecting file names.

While you can abuse the CFileDialog for folder selection by just using the directory part, I would not do so because it is not intended for that and might confuse the users.
Member 13089825 7-May-18 7:59am    
"If you really want folder selection:
SHBrowseForFolder is - as the name suggests - for selecting folders (directories) while CFileDialog is for selecting file names."
i want create a file example test .xml inside some folder,will it possible using file selector
Jochen Arndt 7-May-18 8:07am    
It is possible (and is a file selection).

All you need is the full path to the file name as used in my first code example (dlgFile.GetPathName()). Pass that to any function or class that creates a file like _wfopen(), wfstream, or CFile().

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900