viksoe.dk

CFile class


This article was submitted .


If you have worked with the ATL library 3.0 you have probably been needing a wrapper class for the Windows File functions. A simple class like the CFile class from MFC would cover the most urgent needs.
This wrapper class implements just that.

You get an intuitive interface to files with easy-to-remember access methods like Open() and Create(). And of course the destructor will close the file, so you don't need to remember to call Close().

The following code sample should give you an idea about how it is used:

CFile f;
if( f.Open(szFilename)==TRUE ) {
   FILETIME ftModified;
   f.GetFileTime(NULL, NULL, &ftModified);
   DWORD dwSize = f.GetSize();
   LPSTR pBuffer = (LPSTR) new CHAR[dwSize + 1];
   f.Read(pBuffer, dwSize);
   pBuffer[dwSize] = '\0';
   f.Close();
   ...
   delete [] pBuffer;
}

Interface

 NameDescription
CreateCreates a new file
OpenOpens an existing file.
CloseCloses the file.
ReadReads a number of bytes from the file.
WriteWrites a number of bytes to the file.
SeekSets the file position.
GetPositionReturns the current file position.
FlushFlushes all uncommitted data to the file.
DuplicateHandleDuplicate file handle.
GetSizeReturns the size of the file.
GetFileTimeReturns the file creation-, modification- and access-times.
FileExistsReturns whether a filename exists.
DeleteDeletes a file.
RenameRenames a file.

Source Code Dependencies

Microsoft ATL Library

Download Files

DownloadSource Code (3 Kb)

To the top