VB icon

File I/O class.

Email
Submitted on: 1/1/2015 4:44:00 PM
By: Dennis Wrenn (from psc cd)  
Level: Intermediate
User Rating: By 2 Users
Compatibility: C++ (general), Microsoft Visual C++, Borland C++
Views: 944
 
     a class to make using fstream easier. NOTE: after a call to openfile() it is HIGHLY recomended to call closefile()
 
code:
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
				
//**************************************
// Name: File I/O class.
// Description:a class to make using fstream easier.
NOTE: after a call to openfile() it is HIGHLY recomended to call closefile()
// By: Dennis Wrenn (from psc cd)
//**************************************

#include <fstream>
#include <cstdio>
#include <string>
using namespace std;
const READ = 0;
const WRITE = 1;
/*
ios::app -- Opens the file, and allows additions at the end
ios::ate -- Opens the file, but allows additions anywhere
ios::trunc -- Deletes everything in the file
ios::nocreate -- Does not open if the file must be created
ios::noreplace -- Does not open if the file already exists
*/
const ate = 4;
const app = 8;
const trunc = 16;
const nocreate = 32;
const nereplace = 64;
class file
{
public:
file();
~file();
void openfile(string filename, int readwrite, int mode);
void closefile(int readwrite);
void writefile(string text);
void renamefile(string from, string to);
void deletefile(string filename);
string getcontents();
protected:
ifstream ifile;
ofstream ofile;
};
file::file()
{
}
file::~file()
{
}
void file::openfile(string filename, int readwrite, int mode = nocreate)
{
	switch (readwrite)
	{
		case READ:
			ifile.open(filename.c_str(), mode);
			break;
		case WRITE:
			ofile.open(filename.c_str(), mode);
			break;
	}
}
void file::closefile(int readwrite)
{
	switch (readwrite)
	{
		case READ:
			ifile.close();
			break;
		case WRITE:
			ofile.close();
			break;
	}
}
string file::getcontents()
{
	string str = "";
	char f;
	while(ifile.get(f))
		str += f;
	return str;
}
void file::writefile(string text)
{
	ofile << text;
}
void file::renamefile(string from, string to)
{
	rename(from.c_str(), to.c_str());
}
void file::deletefile(string filename)
{
	remove(filename.c_str());
}


Other 4 submission(s) by this author

 


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this code (in the Intermediate category)?
(The code with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments


 There are no comments on this submission.
 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular code, please click here instead.)
 

To post feedback, first please login.