File Handling
CFileis the base class for Microsoft Foundation file classes. You can use it to manipulate files, like : create/open/read_write/delete/obtain informations/… , similar to the way that you work with ‘fstream’.
You can use ‘CFile’ with ‘CArchive’, to easily store/load from files :
Also with CFile, you can do the same things that you perform with CArchive :
CFile myFile;
Open / Create a file :
myFile.Open( "c:\\test.txt", CFile::modeReadWrite );
The second parameter specifies the access attribute :
Open :
CFile::modeReadWrite or CFile::modeRead or CFile::modeWrite
Create :
CFile::modeCreate (you can use it with ‘CFile::modeNoTruncate‘ (by using bitwise or (|)) to specify that if the file exists, it is not truncated to zero (not cleared), and if not, it will be created)
( There is also some flags for limiting access of file in a sharing environment. )
NOTE : after opening(creating) a file, you should close that :
CFile myFile;
.
.
.
myFile.Close();
Handling Errors :
With passing a ‘CFileException ‘ instance to ‘Open()’ function :
char* pszFileName = "c:\\test\\myfile.dat";
CFile myFile;
CFileException fileException;
if ( !myFile.Open( pszFileName, CFile::modeCreate |
CFile::modeReadWrite ), &fileException )
{
TRACE( "Can't open file %s, error = %u\n",
pszFileName, fileException.m_cause );
}
OR : with CATCHstatement :
char* pFileName = "test.dat";
TRY
{
CFile f( pFileName, CFile::modeCreate | CFile::modeWrite );
}
CATCH( CFileException, e )
{
#ifdef _DEBUG
afxDump << "File could not be opened " << e->m_cause << "\n";
#endif
}
END_CATCH
Write / Read :
Write :
CFile myFile;
char szBuffer[] = "Something for writing";
myFile.Open( "c:\\test.txt", CFile::modeWrite | CFile::modeCreate );
myFile.Write( szBuffer, sizeof(szBuffer)-1 );
‘Write()’ writes some bytes to a file. The first parameter is a buffer to be written in the file, the second is number of bytes(characters) to be written. In the above example, we have ‘-1’ in the second parameter, since we don't want to write the null termination character.
Read :
char szBuffer[11];
UINT nActual = 0;
CFile myFile ( "c:\\test.txt", CFile::modeRead );
nActual = myFile.Read( szBuffer, 10 );
‘Read()’ reads some bytes from a file. The first parameter is a buffer to retrieve data from the file, the second is number of bytes(characters) to be read (it doesn’t add a NULL at the end of the buffer).
‘Read()’ returns the number of bytes that actually reads. Sometimes you specify an amount larger than file’s amount, then it reads smaller bytes.
Seeking :
Moving the file pointer, that indicates where the next read or wirte in the file will occur.
General :
myFile.Seek( 2, CFile::begin );
The first parameter is number of bytes to move the pointer, the second specify where the movement should begin(offset), then the above example move the pointer 2 bytes forward, from the beginning of file.
myFile.Seek( -3, CFile::end );
moves the pointer 3 bytes backward, from the end of file.
Another option for second parameter is CFile::current that indicates the current position of pointer.
Return value : If the requested position is legal, returns the new byte offset from the beginning of the file. Otherwise, the return value is undefined and a CFileException object is thrown.
Begin :
myFile.SeekToBegin();
End :
DWORD file_size = myFile.SeekToEnd();
Return value : Length of the file in bytes.
Obtaining the current position of file pointer :
DWORD dwPosition = myFile.GetPosition();
Obtains the current position of file pointer, which can be used in subsequent calls to ‘Seek()’.
Other Methods:
Obtaining Name / Title / Path of a file :
( If the path of file is : “c:\test\file.txt” )
Name :
CString name = myFile.GetFileName();
‘name ‘ will contain : “file.txt”
Title :
CString title = myFile.GetFileTitle();
‘title ‘ will contain : “file”
Path :
CString path = myFile.GetFilePath();
‘path ‘ will contain : “c:\test\file.txt”
There are some static methods :
(that you can use them with ‘CFile::’ (without an instance of CFile))
Status Informations :
You can obtain some infos about the file : date created/modified/accessed , attribute, size and name. The ‘GetStatus()’ does it for you :
CFileStatus status;
BOOL res = myFile.GetStatus( status );
‘GetStatus()’takes a CFileStatus structure that will receive the status information.
You can also set the status with ‘SetStatus()’:
CFileStatus status;
CFile::SetStatus( “C:\\test.dat”, status );
Remove /Rename :
Very simple :
CFile::Remove( “C:\\test.dat” );
CFile::Rename( “C:\\old_name.dat”, “C:\\new_name.dat” );