CFile::SetFilePath

Call this function to specify the path of the file; for example, if the path of a file is not available when a CFile object is constructed, call SetFilePath to provide it.

virtual void SetFilePath(
   LPCTSTR lpszNewName 
);

Parameters

  • lpszNewName
    Pointer to a string specifying the new path.

Remarks

Hinweis

SetFilePath does not open the file or create the file; it simply associates the CFile object with a path name, which can then be used.

Example

TCHAR* pstrName = _T("C:\\test\\SetPath_File.dat");

// open a file
HANDLE hFile = ::CreateFile(pstrName, GENERIC_WRITE, FILE_SHARE_READ,
   NULL, CREATE_ALWAYS, 0, NULL);

if (hFile != INVALID_HANDLE_VALUE)
{
   // attach a CFile object to it
   CFile myFile(hFile);

   // At this point, myFile doesn't know the path name for the file
   // it owns because Windows doesn't associate that information
   // with the handle. Any CFileExceptions thrown by this object
   // won't have complete information.

   // Calling SetFilePath() remedies that problem by letting CFile
   // know the name of the file that's associated with the object.

   myFile.SetFilePath(pstrName);

   // write something to the file and flush it immediately
   DWORD dwValue = 1234;
   myFile.Write(&dwValue, sizeof(dwValue));
   myFile.Flush();

   // destroying the CObject here will call ::CloseHandle() on the file
} 

Requirements

Header: afx.h

See Also

Concepts

CFile Class

CFile Members

Hierarchy Chart

CFile::GetFilePath

CFile::CFile