Implementing file writers
Quite frequently, our application might want to store some of its data on the disk. Another typical use case we have already encountered is the downloading of some file from the network into a memory buffer. Here, we implement two variations of the iOStream
interface for the ordinary and in-memory files.
How to do it...
- Let us derive the
FileWriter
class from theiOStream
interface. We add theOpen()
andClose()
member functions on top of theiOStream
interface and carefully implement theWrite()
operation. Our output stream implementation does not use memory-mapped files and uses ordinary file descriptors, as shown in the following code:class FileWriter: public iOStream { public: FileWriter(): FPosition( 0 ) {} virtual ~FileWriter() { Close(); } bool Open( const std::string& FileName ) { FFileName = FileName;
- We split Android and Windows-specific code paths using defines:
#ifdef _WIN32 FMapFile = CreateFile( FFileName.c_str(),GENERIC_WRITE, FILE_SHARE_READ...