tutorials.neonphog.com icon

c++ - MyCopy - C++ Style (Source)

Header Source

Header

mycopy_cpp.h

#ifndef MYCOPY_CPP_H
#define MYCOPY_CPP_H

void mycopy_cpp( const char *sSrc, const char *sDest );

#endif

Source

mycopy_cpp.cpp

#include "mycopy_cpp.h"

#include <iostream>
#include <fstream>

#define BUFSIZE (1024)

using namespace std;

int main(int argc, char *argv[])
{
	if( argc != 3 )
	{
		cout << "usage: mycopy_cpp src dest" << endl;
		return 1;
	}

	mycopy_cpp( argv[1], argv[2] );

	return 0;
}


void mycopy_cpp( const char *sSrc, const char *sDest )
{
	char buf[BUFSIZE];
	fstream fIn( sSrc, ios::binary|ios::in );
	fstream fOut( sDest, ios::binary|ios::out );

	/*TODO:
	 *
	 * 1) Check fIn.is_open() to see if we can read, report any errors.
	 * 2) Check fOut.is_open() to see if we can write, report any errors.
	 *
	 * Ideally we would subclass std::exception, throwing our new exceptions
	 * to be caught above in the main function. That, however, is beyond
	 * the scope of this tutorial.
	 *
	 */

	cout << "Copying " << sSrc << " to " << sDest << "." << endl;

	while( fIn.good() )
	{
		fIn.read( buf, BUFSIZE );
		fOut.write( buf, fIn.gcount() );
	}

	fIn.close();
	fOut.close();
}
tutorials home c++ home