Export (0) Print
Expand All

CFile::GetFileName 

Call this member function to retrieve the name of a specified file.


virtual CString GetFileName( ) const;

The name of the file.

For example, when you call GetFileName to generate a message to the user about the file c:\windows\write\myfile.wri, the filename, myfile.wri, is returned.

To return the entire path of the file, including the name, call GetFilePath. To return the title of the file (myfile), call GetFileTitle.

This code fragment opens the SYSTEM.INI file in your WINDOWS directory. If found, the example will print out the name and path and title, as shown under Output:

{
    // ...
    try
    {
        // try to open the file
        CFile sysFile(_T("C:\\WINDOWS\\SYSTEM.INI"), CFile::modeRead);

        // print out path name and title information
        _tprintf_s(_T("Path is : \"%s\"\n"),
                   (LPCTSTR) sysFile.GetFilePath());
        _tprintf_s(_T("Name is : \"%s\"\n"),
                   (LPCTSTR) sysFile.GetFileName());
        _tprintf_s(_T("Title is: \"%s\"\n"), 
                   (LPCTSTR) sysFile.GetFileTitle());

        // close the file handle
        sysFile.Close();
    }
    catch (CFileException* pEx)
    {
        // if an error occurs, just make a message box
        pEx->ReportError();
        pEx->Delete();
    }

    // ...
}

Output

Path is : "C:\WINDOWS\SYSTEM.INI"
Name is : "SYSTEM.INI"
Title is: "System"

Community Additions

ADD
Show:
© 2014 Microsoft