fileoperator.cpp

来自「人工智能课程设计之二」· C++ 代码 · 共 84 行

CPP
84
字号
// FileOperator.cpp: implementation of the CFileOperator class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "Decode.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(CString &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;
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?