www.pudn.com > endecipher.rar > FileOperator.cpp


// FileOperator.cpp: implementation of the CFileOperator class. 
// 
////////////////////////////////////////////////////////////////////// 
 
#include "stdafx.h" 
#include "FileOperator.h" 
 
#ifdef _DEBUG 
#undef THIS_FILE 
static char THIS_FILE[]=__FILE__; 
#define new DEBUG_NEW 
#endif 
 
////////////////////////////////////////////////////////////////////// 
// Construction/Destruction 
////////////////////////////////////////////////////////////////////// 
 
CFileOperator::CFileOperator() 
{ 
 
} 
 
CFileOperator::~CFileOperator() 
{ 
 
} 
 
void CFileOperator::Path(CString &path) 
{ 
	CString errors="Errors!"; 
	CString msg="File path error!"; 
	CFileDialog dlg(true,"TXT",NULL,NULL,"Text file(*.txt)|*.TXT|",NULL); 
	if(dlg.DoModal()==IDOK) 
		path=dlg.GetPathName(); 
	else 
		MessageBox(GetFocus(),msg,errors,MB_OK); 
} 
 
bool CFileOperator::Fread(CString & txt,CString path) 
{ 
	CFile file; 
	CString errors="Errors!"; 
	CString errors1="Openning file error!"; 
	CString errors2="Reading file error!"; 
	if(!file.Open(path,CFile::modeRead)) 
	{ 
		MessageBox(GetFocus(),errors1,errors,MB_OK); 
	    return false; 
	} 
	int len=file.GetLength(); 
	char *buffer=new char [len+1]; 
	try{ 
			file.Read(buffer,len); 
	} 
	catch(CFileException *e) 
	{ 
		MessageBox(GetFocus(),errors2,errors,MB_OK); 
		file.Close(); 
		e->Delete(); 
		return false; 
	} 
	buffer[len]='\0'; 
	txt+=buffer; 
	return true; 
} 
 
bool CFileOperator::Fwrite(char *txt,CString path) 
{ 
	CFile file; 
	CString errors="Errors!"; 
	CString errors1="Openning file error!"; 
	CString errors2="writing file error!"; 
	if(!file.Open(path,CFile::modeWrite|CFile::modeCreate)) 
	{ 
		MessageBox(GetFocus(),errors1,errors,MB_OK); 
		return false; 
	} 
	int len=strlen(txt); 
	file.Write(txt,len); 
	file.Close(); 
	return true; 
}