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