Free Web Hosting by Netfirms
Web Hosting by Netfirms | Free Domain Names by Netfirms

Home Library Index Site Map  Links Contact About

Binary File I/O Support


There are several functions included in this particular library.

This file is included in it's entireity for ease of copying. I hope this doesn't reduce it's readability.

FileIO. CPP

// NAME: FileIO.CPP
// TYPE: C++ SOURCE FILE
// SYNOPSIS: Contains functions for opening,
// closing reading and writing
// structs and classes to/from binary files
// DESCRIPTION:
// AUTHOR: R.F. Mitchell
// DATE: 02/01/2000
// EXAMPLES: filiodrv.cpp
// BUGS:
// SEE ALSO:
//////////////////////////////////////////////

#ifndef _FileIO_CPP_
#define _FileIO_CPP_

// INCLUDES
#include<fstream.H >

//---------------------------------------------
// FUNCTION PROTOTYPES

int
OpenBinFileIn(ifstream& fin, char* fname);

int
OpenBinFileOut(ofstream& fout, char* fname);

template <class T>
int WriteStruct(const T&t, ofstream& fout);

template <class T>
int ReadStruct(T& t, ifstream& fin);

//------------------------------------------------------

//////////////////////
// Function:
// int
// OpenBinFileIn(ofstream& fout, char* fname)
// Description: Opens a file for binary input
// Returns: 1 on success 0 on failure
/////////

int
OpenBinFileIn(ifstream& fin, char* fname)
{
fin.open(fname,ios::binary);
if (!fin) return 0;
return 1;
};
//-----------------------------------------------------

//////////////////////
// Function:
// int
// OpenBinFileOut(ofstream& fout, char* fname)
// Description:
// Opens a file for binary output
// Returns: 1 on success 0 on failure
/////////
int
OpenBinFileOut(ofstream& fout, char* fname)
{
int mode =ios::app // all additions at eof
|| ios::trunc // truncate
|| ios::binary; // binary file

fout.open(fname, mode);
if (!fout) return 0;
return 1;
};

//-----------------------------------------------------

//////////////////////
// Function:
// template <class T>
// void WriteStruct(T& t, ofstream& fout)
//
// Description:
// writes struct or class t to open file fout
// Returns: 1 on success 0 on failure
/////////
template <class T>
int
WriteStruct(const T& t, ofstream& fout)
{
if(!fout) return 0;
fout.write(reinterpret_cast<const char*>(&t),sizeof(t));
return 1;
};

//-----------------------------------------------------

//////////////////////
// Function: template <class T>
// void
// ReadStruct(T& t, ifstream& fin)
//
// Description:
// reads struct or class to t from open file fin
// Returns: 1 on success 0 on failure
/////////
template
<class T> int ReadStruct(T& t, ifstream& fin)
{
if(!fin) return 0;
fin.read(reinterpret_cast<char*>(&t), sizeof(t));
return 1;
}

//----------------------------------------------

#endif // _FileIO_CPP_
Note:
Home Library Index Top

STL Database
Arrays
Binary File Input / Output
Character Input
Containers
Standard Template Library
Streams
Templates
Utility Functions
Win32 Programming
Miscellaneous
 

 
copyright notice

Copyright Robert Mitchell. Last Revised : 28 April, 2000

e-mail me
1