📄 textfile.cpp
字号:
/* ==========================================================================
Class : 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.
========================================================================*/
#include "stdafx.h"
#include "TextFile.h"
////////////////////////////////////////
// CTextFile construction/destruction
CTextFile::CTextFile( const CString& ext, const CString& eol )
/* ============================================================
Function : CTextFile::CTextFile
Description : Constructor
Access : Public
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
'\n'.
Usage : Should normally be created on the stack.
============================================================*/
{
m_extension = ext;
m_eol = eol;
}
CTextFile::~CTextFile()
/* ============================================================
Function : CTextFile::~CTextFile
Description : Destructor
Access : Public
Return : void
Parameters : none
Usage : Should normally be created on the stack.
============================================================*/
{
}
////////////////////////////////////////
// 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.
Access : Public
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
Usage : Call to read the contents of a text file
into a "CStringArray".
============================================================*/
{
ClearError();
BOOL result = TRUE;
if( filename.IsEmpty() )
result = GetFilename( FALSE, filename );
if( result )
{
CStdioFile 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.
Access : Public
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
Usage : Call to read the contents of a text file
into a "CString".
============================================================*/
{
contents = _T( "" );
// Error handling
ClearError();
CStdioFile file;
CFileException feError;
BOOL result = TRUE;
if( filename.IsEmpty() )
result = GetFilename( FALSE, filename );
if( result )
{
// Reading the file
if( file.Open( filename, CFile::modeRead, &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.
Access : Public
Return : BOOL - "TRUE" if OK.
"GetErrorMessage"
will return
errors
Parameters : CString& filename - file to
write to
conste CStringArray& contents - contents
to write
Usage : Call to write the contents of a
"CStringArray" to a text file.
============================================================*/
{
// Error handling
ClearError();
CStdioFile 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.
Access : Public
Return : BOOL - "TRUE" if OK.
"GetErrorMessage"
will return
errors
Parameters : CString& filename - file to write to
const CString& contents - contents to write
Usage : Call to write the contents of a string to a
text file.
============================================================*/
{
// Error checking
ClearError();
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, &feError ) )
{
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 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.
Access : Public
Return : BOOL - "TRUE" if OK.
"GetErrorMessage"
will return errors
Parameters : CString& filename - file to write to
const CString& contents - contents to write
Usage : Call to append the contents of a string to
a text file.
============================================================*/
{
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.
If "filename" is empty, the standard file
dialog will be displayed, and - if OK is
selected - "filename" will contain the
selected filename on return.
Access : Public
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -