|
CFile Class |
Description
This class manages the reading from and writing to files.
CFile Members
| Initialisation and Assignment | |
| CFile | CFile(); CFile(HANDLE hFile); CFile(LPCTSTR pszFileName, UINT nOpenFlags);Constructor for CFile. |
virtual const CString& GetFileName() const;Returns the filename of the file associated with this object. GetFilePath
virtual const CString& GetFilePath() const;Returns the full filename including the directory of the file associated with this object. GetFileTitle
virtual const CString& GetFileTitle() const;Returns the filename of the file associated with this object, excluding the path and the file extension. GetHandle
HANDLE GetHandle() const;Converts the CFile to a HANDLE. GetPosition
virtual ULONGLONG GetPosition() const;Returns the current value of the file pointer, which can be used in subsequent calls to Seek. operator HANDLE
operator HANDLE() const;Converts the CFile to a HANDLE. Operations Close
virtual BOOL Close();Closes the file associated with this object. Closed file can no longer be read or written to.. Flush
virtual BOOL Flush();Causes any remaining data in the file buffer to be written to the file. LockRange
virtual BOOL LockRange(ULONGLONG Pos, ULONGLONG Count);Locks a range of bytes in and open file. Open
virtual int CollateNoCase(LPCTSTR pszText) const;Prepares a file to be written to or read from. OpenFileDialog
virtual CString OpenFileDialog(LPCTSTR pszFilePathName = NULL, DWORD dwFlags = OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT, LPCTSTR pszTitle = NULL, LPCTSTR pszFilter = NULL, CWnd* pOwnerWnd = NULL);Displays the file open dialog. Returns a CString containing either the selected file name or an empty CString. Read
virtual UINT Read(void* pBuf, UINT nCount);Reads from the file, storing the contents in the specified buffer. Remove
static BOOL Remove(LPCTSTR pszFileName);Deletes the specified file. Rename
static BOOL Rename(LPCTSTR pszOldName, LPCTSTR pszNewName);Renames the specified file. SaveFileDialog
virtual CString SaveFileDialog(LPCTSTR pszFilePathName = NULL, DWORD dwFlags = OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT, LPCTSTR pszTitle = NULL, LPCSTR pszFilter = NULL, LPCTSTR pszDefExt = NULL, CWnd* pOwnerWnd = NULL);Displays the SaveFileDialog. Returns a CString containing either the selected file name or an empty CString. Seek
virtual ULONGLONG Seek(LONGLONG lOff, UINT nFrom);Positions the current file pointer. Permitted values for nFrom are: FILE_BEGIN, FILE_CURRENT, or FILE_END. SeekToBegin
virtual void SeekToBegin();Sets the current file pointer to the beginning of the file. SeekToEnd
virtual ULONGLONG SeekToEnd();Sets the current file pointer to the end of the file. SetFilePath
virtual void SetFilePath(LPCTSTR pszNewName);Specifies the full file name, including its path SetLength
virtual BOOL SetLength(ULONGLONG NewLen);Changes the length of the file to the specified value. UnlockRange
virtual BOOL UnlockRange(ULONGLONG Pos, ULONGLONG Count);Unlocks a range of bytes in an open file. Write
virtual BOOL Write(const void* pBuf, UINT nCount);Writes the specified buffer to the file.
Remarks
The following code demonstrates how to use OpenDileDialog to retrieve the name of the file to open.
void CMainFrame::OnFileOpen()
{
CFile File;
CString str = File.OpenFileDialog(0, OFN_FILEMUSTEXIST, _T("Scribble Files (*.dat)\0*.dat\0\0"), this);
if (!str.IsEmpty())
{
// Retrieve the PlotPoint data
m_View.FileOpen(str);
}
}The following code demonstrates how to open a file for reading, and read
its contents.BOOL CView::FileOpen(LPCTSTR szFilename)
{
DWORD nBytesRead;
BOOL bResult = FALSE;
// Create a handle to the file
CFile File;
if (File.Open(szFilename, OPEN_EXISTING))
{
do
{
nBytesRead = 0;
PlotPoint pp;
nBytesRead = File.Read(&pp;, sizeof(PlotPoint));
if (nBytesRead == sizeof(PlotPoint))
m_points.push_back(pp);
} while (nBytesRead == sizeof(PlotPoint));
}
}
Summary Information
| Header file | file.h |
| Win32/64 support | Yes |
| WinCE support | Yes |