  | 
   Binary IO Class 
   Submitted by  |   
  
  
This is a stream wrapper around the standard iostream streams that allows binary
I/O using the <<, >> operators..
  
The classes 
----------- 
 
The output stream is called bofstream, and the input stream is bifstream. Both
are inherited from a bfstream_aux class that provides some common functions
between the streams.
The functions provided are similar to the standard streams, unless stated. Note,
not all the stream functions are available as they do not make sense for binary
I/O..
  Summary of I/O 
--------------- 
 
Output style
==============
simple types like int, float..	- use <<
string and buffers		- use << but must delim them using ends
stl strings			- use << but must delim them using ends
arrays and pointers array	- use write but you can loop through each element and use << (preferred)
class objects			- declare a bofstream friend operator<< justlike the std streams
  Input style
============
simple types like int, float	- use >>
arrays and pointers array	- use read but can use a loop and use >>
string and char buffers		- use getline, not >>. You have been warned!
stl strings			- use >> or getline. String must be delim using ends
class objects			- declare a bifstream friend operator >> just like the std streams
 
  There are a few other functions that you can use to do I/O (like get(),..).
Note: For string data output, you need to specify the terminated the string
explicitly
Eg.
output_stream << "A string" << ends;		// terminated the string using ends
Take a look at the BinaryTest section in the sample for some examples of binary
I/O using <<,>>.
  Doing more 
----------- 
 
  Another added feature is that you can do I/O in both text and binary mode with
the same piece of code.
  All that is needed is you format your output for text output using these
manipulators.
 
 
_	- inserts a space
t_	- inserts a tab
endl	- inserts a newline. Does not flush the stream like the standard stream
 
  The above manipulators does nothing in binary mode, but in text mode, the output
will be formatted correctly.
 
 
Example
  bofstream os("output.txt");
  os << 3 << _ << 4 << _ 5 << endl;	// 3 4 5 newline
  os 	<< 6 endl
	<< 7 endl
	<< 8 endl;			// 6 7 8 on a newline each
  os << "This is a string" << ends;	// string must be terminated by a ends
 
  Output.txt will be
 
 
3 4 5
6
7
8
This is a string
 
  The loader can be written as follows
	bifstream is("output.txt");
  	int i;
  	is >> i >> i >> i;		// read 3 4 5
  	is >> i >> i >> i ;		// reads 6 7 8
  	string s;
  	is >> s;			// reads "This is a string"
  If you want to do the output in binary mode, all that is needed is to specify
the ios::binary flag when opening the file stream. The writer/loader does not
need to change.
e.g. 
bofstream os("output.txt", ios::binary);
bifstream is("output.txt", ios::binary);
  
Some restrictions for this to work properly 
------------------------------------------- 
 
  - Each output entry must be seperated by a space, tab or a newline. If you
output without a seperator, then the loader may load them as a single entry,
instead of two
Example
output_stream << 7 << 8 << endl;		// output without a seperator
  input_stream >> i;				// reads 78 - incorrect
 
  - Each string must be start on a newline and terminated with using ends. So
before writing a string, make sure the previous output has called endl. ends
will insert a newline automatically so the next entry will always have a
seperator.
  
- Each block of data using write() must start on a newline (same restriction as
the string). The write() function will insert a newline automatically after
writing the block. Note that the output in text mode is garbage if you use
write(). It is suggested to write out the elements individually using << for
readability.
  
- Certain functions require caution while using them. For example, the values in
seeking functions are going to be different depending on whether the file is in
text or binary mode. Some functions like putback() should not be used at all!
  The TestText section of the sample has some examples of formatting the data.
 
 
  Some things to take note 
------------------------- 
 
- Do not use >> to read string types unless the storage is a std::string and it
is deliminted using ends. Use getline instead as >> relies on the sizeof(type)
to read in data.
  eg.
char buffer[80];
  // input_stream >> buffer;		<-- Never do this! buffer is not of type std::string
input_stream.getline(buffer, 80);	<-- this should be used for C
strings
 
  
- Don't output a file in text mode and try read it in as binary mode or vice
versa. It won't work! Both file modes must be the same for I/O.
- If you edit the output file that was opened in text mode, you need to save it
in a "Text only" format. Saving the file in notepad will not work because
notepads automatically adds additional '\r' and '\n'when you save. You have to
use Wordpad to edit the output file.
- You can free to use/modify the code for your own use but I will am not
responsible for anything, whether direct or indirect, as a result of using this
wrapper. If you do find a bug, bug me at darkaurora@yahoo.com
 | 
 
 
 
Currently browsing [bftest.zip] (34,525 bytes) - [bftest/bfstream.h] - (13,610 bytes)
 
 /*	A binary file I/O class
  	Desc:	A binary file class that uses <<,>> rather than write/read to perform file stream I/O
  	Author:		Sobeit Void <darkaurora@yahoo.com>
	Revision:	1.0
	Date:		20/11/2000
  	The source code is provided as it is, without any warranty or liability. You are free to modify the
	code for your own use.The author is not liable for any damages resulting from the usage of the code.
  	Output Summary:
  		simple types like int, float,..	- use <<
		string and buffers				- use << as long as they are terminated. Terminate them by ends
		stl strings						- use <<. Terminate them be ends
		arrays and pointers				- use write but you can loop through each element and use <<
		class objects					- use a bofstream friend operator<< just like the std streams
  	Input Summary:
  		simple types like int, float,..	- use >>
		arrays and pointers				- use read but can use a loop and use >>
		string and buffers				- use getline, not >>. You have been warned!
		stl strings						- use >> (if deliminter is '\0') or else use getline
		class objects					- use a bifstream friend operator >> just like the std streams
  	Formatting manipulators (Non standard)
  		_								- inserts a space if text mode. Does nothing in binary mode
		t_								- inserts a tab if text mode. Does nothing in binary mode
		endl							- inserts a newline if text mode. Does nothing in binary mode
		ends							- inserts a '\0' char. Inserts a newline if text mode
  	Avoid using these manipulators in text mode
  		putback()
		peek()
		ignore()
		gcount()
		seeking operators like seekp(), seekg(), tellp() etc. unless the seek is to beginning or end only
*/
  #pragma once
  #include <fstream>
#include <string>
#include <limits>
  //namespace V
//{
//////////////////////////////
//--------------------------------------------------------------------------------------
//	Auxilary class to provide common functions in bofstream and bifstream class and is
//	meant to be instantiated
//--------------------------------------------------------------------------------------
class	bfstream_aux
{
	typedef	std::ios					ios;
	typedef	std::fstream				fstream;
	typedef	std::string					string;
	typedef	std::ios::open_mode			open_mode;
	typedef	std::ios_base::fmtflags		fmtflags;
	typedef	std::ios_base::iostate		iostate;
  public:
  	bool is_binary()	const	{	return bBinary_;		}		// returns true if binary mode
	void close()
	{
		if (fs_.is_open())
			fs_.close();
	}
  	// Operator for error testing
	//////////////////////////////
	bool operator!()	const	{	return !fs_;			}
  	bool is_open()		const	{	return fs_.is_open();	}
  	bool good()			const	{	return fs_.good();		}
  	bool eof()			const	{	return fs_.eof();		}
  	bool fail()			const	{	return fs_.fail();		}
  	bool bad()			const	{	return fs_.bad();		}
  	iostate	rdstate()	const	{	return fs_.rdstate();	}
  	void clear(const iostate state = ios::goodbit)	{	fs_.clear(state);		}
  	void setstate(const iostate state)				{	fs_.setstate(state);	}
  	// Formatting specifiers
	/////////////////////////
	fmtflags	setf(const fmtflags flags)						{	return fs_.setf(flags);			}
	fmtflags	setf(const fmtflags flags, const fmtflags mask)	{	return fs_.setf(flags, mask);	}
  	void		unsetf(const fmtflags mask)		{	fs_.unsetf(mask);			}
  	fmtflags	flags()					const	{	return fs_.flags();			}
	fmtflags	flags(const fmtflags flags)		{	return fs_.flags(flags);	}
  protected:
  	// Constructors made protected as this class is not meant to be instantiated
	bfstream_aux(const string &name, const open_mode mode)
		: fs_(name.c_str(), Set_Flag(mode))		{}
  	bfstream_aux() : fs_(), bBinary_(false)		{}
  	//@@ Closes and reopen the file stream
	void open_stream(const string &name, const open_mode mode)
	{
		close();
		fs_.open(name.c_str(), Set_Flag(mode));
	}
  private:
  	//@@ Sets internal binary flag on/off based on creation flags
	//@@ Remarks: To be called at during opening of the file stream
	open_mode	Set_Flag(const open_mode mode)
	{
		bBinary_ = (ios::binary & mode) ? true : false;	// if binary mode is specified in the passed flags, set flag on
		return mode;									// return the original passed parameters
	}
  	// =================== data ==================
protected:
  	bool		bBinary_;		// true if IO is in binary mode
	fstream		fs_;			// the file stream
};
  /////////////
//------------------------------------------------------------------------------------------
//	The input class
//------------------------------------------------------------------------------------------
class	bifstream :	virtual public	bfstream_aux
{
	typedef	std::ios					ios;
	typedef std::numeric_limits<int>	limit_int;
	typedef std::streamsize				streamsize;
	typedef	std::string					string;
	typedef	std::ios::open_mode			open_mode;
	typedef std::ios::pos_type			pos_type;
	typedef std::ios_base::seekdir		seekdir;
  public:
  	// constructors
	////////////////
	bifstream(const string &name, const open_mode mode = 0)
		: bfstream_aux(name, mode | ios::in)	{}				// text and in mode implied
	bifstream() : bfstream_aux()		{};
  	void open(const string &name, const open_mode mode = 0)		// text mode always implied
	{
		open_stream(name, mode | ios::in);						// in mode always implied
	}
  	// input operators
	///////////////////
	template <typename T>
		bifstream& operator>>(T& data)
	{
		if (bBinary_)								// if binary mode
			fs_.read((char *)&data, sizeof(data));
		else										// text mode
			fs_ >> data;
  		return *this;
	}
  	// template specialization for std::string type
	bifstream& operator>>(string &s)
	{
		text_skip_to_newline();
  		std::getline(fs_, s, '\0');		// the string must be deliminted by a ends
		return *this;
	}
  	template <typename T>
		bifstream& read(T& data, const streamsize size)
	{
		text_skip_to_newline();
  		fs_.read((char *)data, size);
		return *this;
	}
  	bifstream&	getline(char *buffer, const int count, const char delim = '\0')
	{
		text_skip_to_newline();
  		fs_.getline(buffer, count, delim);
		return *this;
	}
  	bifstream&	getline(string &str, const char delim = '\0')
	{
		text_skip_to_newline();
  		std::getline(fs_, str, delim);
		return *this;
	}
  	//@@ Remarks: Avoid usage in text mode
	streamsize	gcount()	const	{	return fs_.gcount();	}
  	//@@ Remarks: Avoid usage in text mode
	bifstream& ignore(const streamsize count = 1, const int delim = std::char_traits<char>::eof())
	{
		fs_.ignore(count, delim);
		return *this;
	}
  	//@@ Remarks: Avoid usage in text mode
	int	peek()			{	return fs_.peek();	}
  	//@@ Remarks: Avoid usage in text mode
	bifstream&	putback(char c)		{	fs_.putback(c);		return *this;	}
  	// seeking operators - exercise caution when using in text mode
	///////////////////////////////////////////////////////////////
	bifstream&	seekg(const pos_type pos)						{	fs_.seekg(pos);			return *this;	}
	bifstream&	seekg(const pos_type pos, const seekdir dir)	{	fs_.seekg(pos, dir);	return *this;	}
  	pos_type	tellg()		{	return fs_.tellg();		}
  private:
  	//@@ Moves the file position to a newline in if the file is in text mode. Function does nothing in binary mode
	void text_skip_to_newline()
	{
		if (!bBinary_)
			fs_.ignore(limit_int::max(),'\n');		// ignore everything until a newline is met
	}
  };
  ///////////////////////
//--------------------------------------------------------------------------------------------
//	The output class
//--------------------------------------------------------------------------------------------
class	bofstream	:	virtual public bfstream_aux
{
	typedef	std::ios					ios;
	typedef std::streamsize				streamsize;
	typedef	std::string					string;
	typedef	std::ios::open_mode			open_mode;
	typedef std::ios::pos_type			pos_type;
	typedef std::ios_base				ios_base;
	typedef std::ios_base::seekdir		seekdir;
  public:
  	// constructors
	////////////////
	bofstream(const string &name, const open_mode mode = std::ios::trunc)
		: bfstream_aux(name, mode | ios::out)		{}				// trunc and out mode implied
	bofstream() : bfstream_aux()					{};
  	void open(const string &name, const open_mode mode = ios::trunc)	// trunc mode implied
	{
		open_stream(name, mode | ios::out);								// out mode always implied
	}
  	// output operators
	////////////////////
	// output for basic types
	template <typename T>
		bofstream& operator<<(const T& data)
	{
		if (bBinary_)										// if binary mode
			fs_.write((const char *)&data, sizeof(data));
		else												// text mode
			fs_ << data;
  		return *this;
	}
  	// template specialization for char buffers as sizeof() doesn't work with string types
	// traps		string array			- char buffer[20]
	//				pointer string			- char *p = new char[20]
	//				const string			- "Const string"
	// parameter cannot be const to trap pointer string
	bofstream& operator<<(char *data)
	{
		fs_.write(data, strlen(data));		// write out the length of the string
		return *this;
	}
  	// template specialization for const string pointers and const string const pointers
	// traps		const string pointers		- const char *p = "Const string"
	//				const string const pointers	- const char * const p = "Const string"
	bofstream& operator<<(const char *data)
	{
		fs_.write(data, strlen(data));		// write out the length of the string
		return *this;
	}
  	// template specialization for std::string type
	bofstream& operator<<(const string &s)
	{
		fs_.write(s.c_str(), s.length());	// write out the length of the string
		return *this;
	}
  	template <typename T>
		bofstream&	write(const T &data, const streamsize size)
	{
		fs_.write((const char *)data, size);
  		textput('\n');					// if text mode, place a newline to make a new block
		return *this;
	}
  	bofstream&	put(const char c)		{	fs_.put(c);		return *this;	}
  	//@@ Outputs the passed char if the file is in text mode. Function does nothing in binary mode.
	void	textput(const char c)
	{
		if (!bBinary_)			// if not binary mode, then do the output
			fs_.put(c);
	}
  	// Seeking operators - exercise caution when using in text mode
	///////////////////////////////////////////////////////////////
	bofstream&	seekp(const pos_type pos)						{	fs_.seekp(pos);			return *this;	}
	bofstream&	seekp(const pos_type pos, const seekdir dir)	{	fs_.seekp(pos, dir);	return *this;	}
  	pos_type	tellp()			{	return fs_.tellp();		}
  	// manipulators
	////////////////
	// Function pointer for passing in endl, ends, flush
	bofstream& operator<<(void (*fp)(bofstream&))	{	(*fp)(*this);	return *this;	}
  	bofstream&	flush()								{	fs_.flush();	return *this;	}
  	streamsize	precision()					const	{	return fs_.precision();			}
	streamsize	precision(const streamsize size)	{	return fs_.precision(size);		}
  	streamsize	width()						const	{	return fs_.width();				}
	streamsize	width(const streamsize size)		{	return fs_.width(size);			}
};
  ///////////////////
// manipulators
///////////////
inline void flush(bofstream& s)		{	s.flush();		}
  inline void ends(bofstream& s)		{	s.put('\0');	s.textput('\n');	}
  // text mode manipulators - only outputs the char in text mode, does nothing in binary mode
inline void endl(bofstream& s)		{	s.textput('\n');	}		// puts a newline in text mode only, no flush, unlike standard streams
inline void _(bofstream& s)			{	s.textput(' ');		}		// puts a space in text mode only
inline void t_(bofstream& s)		{	s.textput('\t');	}		// puts a tab in text mode only
////////////////////
//----------------------------------------------
// auxilary class for parameterized manipulators
//----------------------------------------------
template <typename T>
class Bfmanip
{
	typedef void(*FP)(bofstream&, T);		// function pointer prototype
public:
	Bfmanip(FP fp , T arg) : fp_(fp), arg_(arg)	{};
  	friend bofstream& operator<<(bofstream& os, const Bfmanip &manip)
	{
		(*manip.fp_)(os, manip.arg_);
		return os;
	}
  	friend bifstream& operator>>(bifstream& is, const Bfmanip &manip)
	{
		(*manip.fp_)(is, manip.arg_);
		return is;
	}
  	// =========== data ===============
private:
  	FP		fp_;		// function pointer
	T		arg_;		// arg passed to function pointer
};
  // auxiliary functions for parameterized manipulators
/////////////////////////////////////////////////////
inline void setiosflags__(bofstream& os, std::ios_base::fmtflags m)		{	os.setf(m);					}
inline void resetiosflags__(bofstream& os, std::ios_base::fmtflags m)	{	os.setf(os.flags(), m);		}
  inline void setprecision__(bofstream& os, const int i)		{	os.precision(i);	}
inline void setw__(bofstream& os, const int i)				{	os.width(i);		}
  // parameterized manipulators that affect text mode only
/////////////////////////////////////////////////////////
inline Bfmanip<std::ios_base::fmtflags> setiosflags(std::ios_base::fmtflags mask)	
{
	return Bfmanip<std::ios_base::fmtflags>(setiosflags__, mask);
}
  inline Bfmanip<std::ios_base::fmtflags> resetiosflags(std::ios_base::fmtflags mask)
{
	return Bfmanip<std::ios_base::fmtflags>(resetiosflags__, mask);
}
  inline Bfmanip<const int>	setprecision(const int prec)	{	return Bfmanip<const int>(setprecision__, prec);	}
  inline Bfmanip<const int>	setw(const int width)			{	return Bfmanip<const int>(setw__, width);			}
  ///////////////////////////////
//};	// end namespace   |  
  
 | 
 
  
Currently browsing [bftest.zip] (34,525 bytes) - [bftest/BinaryTest.cpp] - (7,115 bytes)
 
 /* Binary I/O test */
  #include "BinaryTest.h"
  #include "bfstream.h"		// the new file stream
#include <iostream>
  using namespace std;
  ////////
//	This tests outputs two files in binary mode using the standard stream (old.txt)
//	and the bofstream.(new.txt)
//	Use file compare (fc /b) to compare the two files
void Output_Binary()
{
	int index;			// looping var
	cout << "Binary output" << endl;
	cout << "--------------" << endl;
  	ofstream	ofs("old.txt", ios::binary);		// a normal ofstream
	bofstream	bos("new.txt", ios::binary);		// the wrapper file stream in binary mode
	if (!ofs || !bos)
	{
		cout << "*******************************************************" << endl;
		cout << "*****************Error creating file*******************" << endl;
		cout << "*******************************************************" << endl;
	}
  	// basic type output
	//------------------
	int		i	= 987;
	float	f	= -12.34f;
	double	d	= 87654321.141231234;
  	ofs.write((const char *)&i, sizeof(i));		// old way
	ofs.write((const char *)&f, sizeof(f));
	ofs.write((const char *)&d, sizeof(d));
  	bos << i << f << d;							// write basic types like this
	cout << i << " " << f << " " << d << endl;
  	// basic array output
	//--------------------
	float f_a[5];
  	for (index = 0; index < 5; ++index)
		f_a[index]	= -123.23f - index;
  	ofs.write((const char *)f_a, sizeof(float)*5);	// old way
	bos.write(f_a, sizeof(float)*5);				// write arrays like this (no cast needed) but appears as garbage in text mode
	for (index = 0; index < 5; ++index)
		cout << f_a[index] << " ";
	cout << endl;
  	// basic pointer output
	//----------------------
	float *f_p = new float[5];
  	for (index = 0; index < 5; ++index)
		f_p[index] = 98.32f - index;
  	ofs.write((const char *)f_p, sizeof(float)*5);		// old way
	for (index = 0; index < 5; ++index)					// slower to write pointer arrays like this
		bos << f_p[index];								// but values are viewable in text mode
	for (index = 0; index < 5; ++index)
		cout << f_p[index] << " ";
	cout << endl;
  	delete	[]f_p;
	f_p	= NULL;
  	// string output - char buffer
	//-----------------------------
	char buffer[20]	= "Char buffer";			// a char buffer
	ofs << buffer << ends;
  	bos	<< buffer << ends;						// write a string(term by a '\0')
	cout << buffer << endl;
  	// string output - raw char pointer
	//---------------------------------
	char *c_p	= new char[20];
  	sprintf(c_p,"Char pointer");
  	ofs << c_p << ends;
  	bos << c_p << ends;							// write a string(term by a '\0')
	cout << c_p << endl;
  	delete	[]c_p;
	c_p = NULL;
  	// constant string
	//-----------------
	ofs		<< "Const string" << ends;
  	bos		<< "Const string" << ends;			// write a const string(term by a '\0')
	cout	<< "Const string" << endl;
  	// const string pointers
	//----------------------
	const char* const_p = "Const char string";
  	ofs		<< const_p << ends;
	bos		<< const_p << ends;					// write a const string pointer(term by a '\0')
	cout	<< const_p << endl;
  	const char* const cc2_p = "const char const string";
  	ofs		<< cc2_p << ends;
	bos		<< cc2_p << ends;					// write a const string const pointer(term by a '\0')
	cout	<< cc2_p << endl;
  	// STL string
	//-----------
	string stl_string	= "STL string";
  	ofs		<< stl_string << ends;
	bos		<< stl_string << ends;					// write a stl std::string
	cout	<< stl_string << endl;
  	// char output
	//------------
	char c = 'A';
  	ofs.put(c);
	bos.put(c);									// output the char
	cout << c << endl;
  	cout << "ofstream tellp - " << ofs.tellp() << " bofstream tellp - " << bos.tellp() << endl;		// output the file position
	if (!bos || !ofs)
	{
		cout << "**********************************************************" << endl;
		cout << "******************Error writing to file*******************" << endl;
		cout << "**********************************************************" << endl;
	}
}
  ////////////////////////////////
void Input_Binary()
{
	int index;			// looping var
	cout << "Binary Input" << endl;
	cout << "------------" << endl;
  	bifstream	is("new.txt", ios::binary);
  	if (!is)
	{
		cout << "*********************************************************" << endl;
		cout << "**********************Error reading file*****************" << endl;
		cout << "*********************************************************" << endl;
	}
  	// basic type input
	//------------------
	int		i;
	float	f;
	double	d;
  	is >> i >> f >> d;							// read basic types like this
	cout << i << " " << f << " " << d << endl;
  	// basic array input
	//-------------------
	float f_a[5];
  	is.read(f_a, 5*sizeof(float));				// read an array like this (no cast needed)
	for (index = 0; index < 5; ++index)
		cout << f_a[index] << " ";
	cout << endl;
  	// basic pointer input
	//--------------------
	float *f_p	= new float[5];
  	for (index = 0; index < 5; ++index)			// This is the slower way of reading an array
		is >> f_p[index];
  	for (index = 0; index < 5; ++index)
		cout << f_p[index] << " ";
	cout << endl;
  	delete	[]f_p;
	f_p	= NULL;
  	// string input - char buffer
	//---------------------------
	char buffer[20];
  	//	is >> buffer;					// Never use >> to read char *!!!!!, should use get or getline
	is.getline(buffer, 20);				// use getline read in a string (term char is '\n')
	cout << buffer << endl;
  	// string input - raw pointer
	//----------------------------
	char *c_p = new char[20];
  	is.getline(c_p, 20);
  	cout << c_p << endl;
  	delete	[]c_p;
	c_p	= NULL;
  	// string input - const string
	//----------------------------
	char const_buf[20];
  	is.getline(const_buf, 20);			// read in the value outputted by a const string(term char is '\n')
	cout << const_buf << endl;
  	// const string pointer
	//---------------------
	string const_s1;
  	is.getline(const_s1);				// better way to store strings (term char is '\0') - no more worries about buffer overrun
	cout << const_s1.c_str() << endl;
  	string stl_s2;
  	is >> stl_s2;						// a even better way to read a string into a stl std::string
	cout << stl_s2.c_str() << endl;
  	// STL string
	//------------
	string	stl_string;
  	is >> stl_string;					// read in the output by a STL string
	cout << stl_string.c_str() << endl;
  	// single char operations
	//--------------------------
	char c;
  	is >> c;
	cout << "Getting a char " << c << endl;
  	//	is.putback(c);					// put back the extracted character
	//	is >> c;						// a better way to extract a char
	//	cout << "Putting it back and retriving char " << c << endl;
	cout << "bifstream tellg - " << is.tellg() << endl;	// output the file position
	if (!is)
	{
		cout << "*************************************************************" << endl;
		cout << "*****************Error reading from file*********************" << endl;
		cout << "*************************************************************" << endl;
	}
}   |  
  
 | 
 
  
Currently browsing [bftest.zip] (34,525 bytes) - [bftest/BinaryTest.h] - (84 bytes)
 
 /* Binary I/O Test */
  extern void Output_Binary();
  extern void Input_Binary();   |  
  
 | 
 
  
Currently browsing [bftest.zip] (34,525 bytes) - [bftest/main.cpp] - (237 bytes)
 
 #include "BinaryTest.h"
#include "TextTest.h"
  void main()
{
	// do binary I/O test
	//-------------------
	Output_Binary();
	Input_Binary();
  	// do text I/O test
	//-----------------
	//	Output_Text();
	//	Input_Text();
}   |  
  
 | 
 
  
Currently browsing [bftest.zip] (34,525 bytes) - [bftest/TextTest.cpp] - (4,392 bytes)
 
 /* Text I/O test */
  #include "TextTest.h"
  #include "bfstream.h"
  #include <iostream>
#include <string>
  using namespace std;
  ///////////////////
void Output_Text()
{
	int	index;
  	cout <<	"Output Text" << endl;
	cout << "------------" << endl;
  	bofstream os("text.txt");					// output in text mode
	//	bofstream os("text.txt", ios::binary);		// output in binary mode
	if (!os)
	{
		cout << "*******************************************************" << endl;
		cout << "******************* Error creating file****************" << endl;
		cout << "*******************************************************" << endl;
	}
  	int		i	= 123;
	float	f	= 456.78f;
	double	d	= 9101.123456678;
  	os << setprecision(10) << setw(10); 
  	os << i << _ << f << t_ << d << endl;				// element seperated by a space and then a tab
	cout << i << " " << f << " " << d << endl;
  	float	f_a[5]	= {		28.01f, 27.01f, 26.01f, 25.01f, 24.01f	};
  	for (index = 0; index < 5; ++index)					// every element on a newline
		os << f_a[index] << endl;
  	for (index = 0; index < 5; ++index)
		cout << f_a[index] << " ";
	cout << endl;
  	os		<< " This is a string" << ends;				// a string. ends will insert a newline automatically
	cout	<< " This is a string" << endl;
  	os		<< "This is a combined string";
	os		<< '\n';
	os		<< "seperated by a newline" << ends;		// combined string. ends will insert a newline automatically
	cout	<< "This is a combined string";
	cout	<< '\n';
	cout	<< "seperated by a newline" << endl;
  	os		<< 'Z' << endl;				// output a char - remember the need for the endl because the next write requires to start on a newline
	cout	<< 'Z' << endl;
  	double d_a[5]	= {		-10.23f, -11.23f, -12.23f, -13.23f, -14.23f	};
  	os.write(d_a, sizeof(double) * 5);					// write a block of memory. A newline will be inserted automatically
	for (index = 0;  index < 5; ++index)
		cout << d_a[index] << " ";
	cout << endl;
  	float f_arr[5]	= {		99.1f, 99.2f, 99.3f, 99.4f, 99.5f	};
  	os.write(f_arr, sizeof(float) * 5);					// write another block. A newline will be inserted automatically
	for (index = 0;  index < 5; ++index)
		cout << f_arr[index] << " ";
	cout << endl;
  	os << 'A' << 'B' << 'C';							// output 3 chars without spacing
	cout << 'A' << " " << 'B' << " " << 'C' << endl;
  	if (!os)
	{
		cout << "*******************************************************" << endl;
		cout << "******************* Error creating file****************" << endl;
		cout << "*******************************************************" << endl;
	}
}
  ////////
void Input_Text()
{
	int index;
  	cout << "Text Input" << endl;
	cout << "-----------" << endl;
  	bifstream is("text.txt");				// input in text mode
	//	bifstream is("text.txt", ios::binary);	// input in binary mode
	if (!is)
	{
		cout << "*******************************************************" << endl;
		cout << "******************* Error reading file ****************" << endl;
		cout << "*******************************************************" << endl;
	}
  	int		i	= 0;
	float	f	= 0;
	double	d	= 0;
  	is >> i >> f >> d;						// read in 3 elements
	cout << i << " " << f << " " << d << endl;
  	float f_a[5]	= { 0.0f };
  	for (index = 0; index < 5; ++index)		// read in another 5 elements
		is >> f_a[index];
  	for (index = 0; index < 5; ++index)
		cout << f_a[index] << " ";
	cout << endl;
  	string s1, s2;
  	is >> s1 >> s2;							// read in two strings
	cout << s1.c_str() << endl;
	cout << s2.c_str() << endl;
  	char c;
  	is >> c;								// read in a char
	cout << c << endl;
  	double d_a[5];
  	is.read(d_a, sizeof(double) * 5);		// read a block of doubles
	for (index = 0; index < 5; ++index)
		cout << d_a[index] << " ";
	cout << endl;
  	float f_arr[5];
  	is.read(f_arr, sizeof(float) * 5);			// read in a block of floats
	for (index = 0; index < 5; ++index)
		cout << f_arr[index] << " ";
	cout << endl;
  	char ch1, ch2, ch3;
  	is >> ch1 >> ch2 >> ch3;					// read int 3 chars
	cout << ch1 << " " << ch2 << " " << ch3 << endl;
  	if (!is)
	{
		cout << "*******************************************************" << endl;
		cout << "******************* Error reading file ****************" << endl;
		cout << "*******************************************************" << endl;
	}
}   |  
  
 | 
 
  
Currently browsing [bftest.zip] (34,525 bytes) - [bftest/TextTest.h] - (78 bytes)
 
 /* Text I/O test */
  extern void Output_Text();
  extern void Input_Text();   |  
  
 | 
 
 
 
The zip file viewer built into the Developer Toolbox made use
of the zlib library, as well as the zlibdll source additions.
 
 
 |