共用方式為


CFile::SetFilePath

呼叫這個函式會指定檔案的路徑,例如,在中,如果檔案的路徑不可用,則 C 檔案 建構物件時,呼叫會提供自己的 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

請參閱

參考

C 檔案類別

階層架構圖

CFile::GetFilePath

CFile::CFile