mrDIMAS

Untitled

Jun 24th, 2015
377
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.46 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3.  
  4. using namespace std;
  5.  
  6. class BinaryIO {
  7. private:
  8.     FILE * mFile;
  9.  
  10.     void InternalRead( string & str ) {
  11.         char symbol = 0;
  12.         do {
  13.             fread( reinterpret_cast<void*>( &symbol ), sizeof( symbol ), 1, mFile );
  14.             if( symbol ) {
  15.                 str.push_back( symbol );
  16.             }
  17.         } while( symbol != 0 );
  18.     }
  19.  
  20.     void InternalRead( int & integer ) {
  21.         fread( reinterpret_cast<void*>( &integer ), sizeof( integer ), 1, mFile );
  22.     }
  23.  
  24.     void InternalRead( float & real ) {
  25.         fread( reinterpret_cast<void*>( &real ), sizeof( real ), 1, mFile );
  26.     }
  27.  
  28.     void InternalRead( char & byte ) {
  29.         fread( reinterpret_cast<void*>( &byte ), sizeof( byte ), 1, mFile );
  30.     }
  31.  
  32.     void InternalRead( short & word ) {
  33.         fread( reinterpret_cast<void*>( &word ), sizeof( word ), 1, mFile );
  34.     }
  35. public:
  36.     enum class Mode {
  37.         Write, Read
  38.     };
  39.  
  40.     explicit BinaryIO( const string & fileName, const Mode & mode ) {      
  41.         Open( fileName, mode );
  42.     }
  43.  
  44.     ~BinaryIO() {
  45.         Close();
  46.     }
  47.  
  48.     void Close() {
  49.         fclose( mFile );
  50.     }
  51.  
  52.     void Open( const string & fileName, const Mode & mode ) {
  53.         if( fopen_s( &mFile, fileName.c_str(), mode == Mode::Write ? "wb" : "rb" )) {
  54.             throw runtime_error( string( "Unable to open file " ) + fileName );
  55.         }
  56.     }
  57.  
  58.     void Write( const string & str ) const {
  59.         static char zero = 0;
  60.         fwrite( str.c_str(), sizeof( char ), str.size(), mFile );      
  61.         fwrite( reinterpret_cast<void*>( &zero ), sizeof( char ), 1, mFile );
  62.     }
  63.  
  64.     void Write( int integer ) const {
  65.         fwrite( reinterpret_cast<void*>( &integer ), sizeof( integer ), 1, mFile );
  66.     }
  67.  
  68.     void Write( float real ) const {
  69.         fwrite( reinterpret_cast<void*>( &real ), sizeof( real ), 1, mFile );
  70.     }
  71.  
  72.     void Write( char byte ) const {
  73.         fwrite( reinterpret_cast<void*>( &byte ), sizeof( byte ), 1, mFile );
  74.     }
  75.  
  76.     void Write( short word ) const {
  77.         fwrite( reinterpret_cast<void*>( &word ), sizeof( word ), 1, mFile );
  78.     }
  79.    
  80.     template< class T > T Read( ) {
  81.         T out;
  82.         InternalRead( out );
  83.         return out;
  84.     }
  85. };
  86.  
  87. void main() {
  88.     BinaryIO io( "file.bin", BinaryIO::Mode::Write );
  89.  
  90.     // write
  91.     io.Write( "This is string" );
  92.     io.Write( 12345 );
  93.     io.Write( 98765.0f );
  94.     io.Write( (char)127 );
  95.     io.Write( (short)16535 );
  96.  
  97.     io.Close();
  98.  
  99.     // read
  100.     io.Open( "file.bin", BinaryIO::Mode::Read );
  101.  
  102.     string str = io.Read<string>();
  103.     int integer = io.Read<int>();
  104.     float real = io.Read<float>();
  105.     char byte = io.Read<char>();
  106.     short word = io.Read<short>();
  107.  
  108.     io.Close();
  109.  
  110.     system( "pause" );
  111. }
Advertisement
Add Comment
Please, Sign In to add comment