⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 textfile.cpp

📁 管理项目进度工具的原代码
💻 CPP
📖 第 1 页 / 共 2 页
字号:
/* ==========================================================================
	CTextFile

	Author :		Johan Rosengren, Abstrakt Mekanik AB

	Date :			2004-03-22

	Purpose :		The class is a helper-package for text files and 
					windows. It allows loading and saving text files in a 
					single operation, as well as getting text to and 
					from edit- and listboxes. If an empty filename is given 
					as a parameter to a call, the standard file dialog will 
					be displayed, to let the user select a file.
					Error handling is managed internally, and the different 
					API-functions return a BOOL to signal success or 
					failure. In case of failure, FALSE returned, the member 
					function GetErrorMessage can be called to retrieve a 
					CString with the error message.
					If this string is empty, the file selection was aborted 
					in the case of an empty input name.
   ========================================================================
					14/4 2005	Added Dave Pritchards class CStdioFileEx
								for MBCS/UNICODE-support.
   ========================================================================*/

#include "stdafx.h"
#include "TextFile.h"
#include "StdioFileEx.h"

////////////////////////////////////////
// CTextFile construction/destruction

CTextFile::CTextFile( const CString& ext, const CString& eol )
/* ============================================================
	Function :		CTextFile::CTextFile
	Description :	constructor
					
	Return :		void
	Parameters :	const CString& ext	-	Standard extension 
											to use in case no 
											file name is given.
					const CString& eol	-	The end-of-line to 
											use. Defaults to 
											"\r\n".

   ============================================================*/
{

	m_extension = ext;
	m_eol = eol;

}

CTextFile::~CTextFile()
/* ============================================================
	Function :		CTextFile::~CTextFile
	Description :	destructor
					
	Return :		void
	Parameters :	none

   ============================================================*/
{
}

////////////////////////////////////////
// CTextFile operations
//

BOOL CTextFile::ReadTextFile( CString& filename, CStringArray& contents )
/* ============================================================
	Function :		CTextFile::ReadTextFile
	Description :	Will read the contents of the file filename 
					into the CStringArray contents, one line 
					from the file at a time.
					If filename is empty, the standard file 
					dialog will be displayed, and - if OK is 
					selected - filename will contain the 
					selected filename on return.

	Return :		BOOL					-	TRUE if OK. 
												GetErrorMessage 
												will contain errors.
	Parameters :	CString& filename		-	file to read from
					CStringArray& contents	-	will be filled 
												with the contents 
												of the file

   ============================================================*/
{

	ClearError();
	BOOL result = TRUE;

	if( filename.IsEmpty() )
		result = GetFilename( FALSE, filename );

	if( result )
	{
		CStdioFileEx file;
		CFileException feError;

		if( file.Open( filename, CFile::modeRead , &feError ) )
		{

			contents.RemoveAll();

			CString line;
			while( file.ReadString( line ) )
				contents.Add( line );

			file.Close();

		}
		else
		{

			TCHAR	errBuff[256];
			feError.GetErrorMessage( errBuff, 256 );
			m_error = errBuff;
			result = FALSE;

		}
	}

	return result;

}

BOOL CTextFile::ReadTextFile( CString& filename, CString& contents )
/* ============================================================
	Function :		CTextFile::ReadTextFile
	Description :	Will read the contents of the file filename 
					into contents.
					If filename is empty, the standard file 
					dialog will be displayed, and - if OK is 
					selected - filename will contain the 
					selected filename on return.
					
	Return :		BOOL				-	TRUE if OK. 
											GetErrorMessage will 
											contain errors.
	Parameters :	CString& filename	-	file to read from
					CString& contents	-	will be filled with 
											the contents of the 
											file

   ============================================================*/
{

	contents = _T( "" );

	// Error handling
	ClearError();

	CStdioFileEx file;
	CFileException feError;
	BOOL result = TRUE;

	if( filename.IsEmpty() )
		result = GetFilename( FALSE, filename );

	if( result )
	{

		// Reading the file
		if( file.Open( filename, CFile::modeRead | CFile::typeText, &feError ) )
		{

			CString line;
			while( file.ReadString( line ) )
				contents += line + m_eol;

			file.Close();

		}
		else
		{

			// Setting error message
			TCHAR	errBuff[256];
			feError.GetErrorMessage( errBuff, 256 );
			m_error = errBuff;
			result = FALSE;

		}
	}

	return result;

}

BOOL CTextFile::WriteTextFile( CString& filename, const CStringArray& contents )
/* ============================================================
	Function :		CTextFile::WriteTextFile
	Description :	Writes contents to filename. Will create 
					the file if it doesn't already exist, 
					overwrite it otherwise.
					If filename is empty, the standard file 
					dialog will be displayed, and - if OK is 
					selected - filename will contain the 
					selected filename on return.
					
	Return :		BOOL							-	TRUE if OK. 
														GetErrorMessage 
														will return 
														errors
	Parameters :	CString& filename				-	file to 
														write to
					conste CStringArray& contents	-	contents 
														to write

   ============================================================*/
{

	// Error handling
	ClearError();

	CStdioFileEx file;
	CFileException feError;
	BOOL result = TRUE;

	if( filename.IsEmpty() )
		result = GetFilename( TRUE, filename );

	if( result )
	{
		// Write file
		if( file.Open( filename, CFile::modeWrite | CFile::modeCreate , &feError ) )
		{

			int max = contents.GetSize();
			for( int t = 0 ; t < max ; t++ )
				file.WriteString( contents[ t ] + m_eol );

			file.Close();

		}
		else
		{

			// Set error message
			TCHAR	errBuff[256];
			feError.GetErrorMessage( errBuff, 256 );
			m_error = errBuff;
			result = FALSE;

		}
	}

	return result;

}

BOOL CTextFile::WriteTextFile( CString& filename, const CString& contents )
/* ============================================================
	Function :		CTextFile::WriteTextFile
	Description :	Writes contents to filename. Will create 
					the file if it doesn't already exist, 
					overwrite it otherwise.
					If filename is empty, the standard file 
					dialog will be displayed, and - if OK is 
					selected - filename will contain the 
					selected filename on return.
					
	Return :		BOOL					-	TRUE if OK. 
												GetErrorMessage 
												will return 
												errors
	Parameters :	CString& filename		-	file to write to
					const CString& contents	-	contents to write

   ============================================================*/
{

	// Error checking
	ClearError();

	CStdioFileEx file;
	CFileException feError;
	BOOL result = TRUE;

	if( filename.IsEmpty() )
		result = GetFilename( TRUE, filename );

	if( result )
	{
		// Write the file
		if( file.Open( filename, CFile::modeWrite | CFile::modeCreate , &feError ) ) 
		{

			file.WriteString( contents );
			file.Close();

		}
		else
		{

			// Set error message
			TCHAR	errBuff[256];
			feError.GetErrorMessage( errBuff, 256 );
			m_error = errBuff;
			result = FALSE;

		}
	}

	return result;

}

BOOL CTextFile::AppendFile( CString& filename, const CString& contents )
/* ============================================================
	Function :		CTextFile::AppendFile
	Description :	Appends contents to filename. Will create 
					the file if it doesn't already exist.
					If filename is empty, the standard file 
					dialog will be displayed, and - if OK is 
					selected - filename will contain the 
					selected filename on return.
					AppendFile will not add eols.
					
	Return :		BOOL					-	TRUE if OK. 
												GetErrorMessage 
												will return errors
	Parameters :	CString& filename		-	file to write to
					const CString& contents	-	contents to write

   ============================================================*/
{

	CFile file;
	CFileException feError;
	BOOL result = TRUE;

	if( filename.IsEmpty() )
		result = GetFilename( TRUE, filename );

	if( result )
	{
		// Write the file
		if( file.Open( filename, CFile::modeWrite | CFile::modeCreate | CFile::modeNoTruncate, &feError ) ) 
		{

			file.SeekToEnd();
			file.Write( contents, contents.GetLength() );
			file.Close();

		}
		else
		{

			// Set error message
			TCHAR	errBuff[256];
			feError.GetErrorMessage( errBuff, 256 );
			m_error = errBuff;
			result = FALSE;

		}
	}

	return result;

}

BOOL CTextFile::AppendFile( CString& filename, const CStringArray& contents )
/* ============================================================
	Function :		CTextFile::AppendFile
	Description :	Appends contents to filename. Will create 
					the file if it doesn't already exist.

⌨️ 快捷键说明

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