Поделиться через


CFile::SetFilePath

Эта функция вызывается для указания пути файла. например, если путь к файлу недоступен, если объект CFile построен, вызов SetFilePath, который необходимо обеспечить его.

virtual void SetFilePath(
   LPCTSTR lpszNewName 
);

Параметры

  • lpszNewName
    Указатель на строку, определяющий новый путь.

Заметки

ПримечаниеПримечание

SetFilePath не открывает файл или не создает файл; он просто связывает объект CFile с именем пути, который можно затем использовать.

Пример

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
} 

Требования

Header: afx.h

См. также

Ссылки

Класс CFile

Диаграмма иерархии

CFile::GetFilePath

CFile::CFile