Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

CTextFile: A handy helper

0.00/5 (No votes)
30 Mar 2005 1  
A small helper class for reading, writing and appending to/from text files.

Introduction

While perhaps not the most impressive of classes, this one is at least useful. It lets you read, write and append to text files from a CString or CStringArray, as well as load and save to and from list- and editboxes. While not what you need to manage your data in an application, it can be used for configuration files, text dumps during development, and other such small tasks. And - it will pop up a file dialog if no file name is given to any of the member calls.

Using the code

Instantiate a CTextFile and fire away! Here is an example:

CString str("");
CTextFile tf("gnu");
  if( !tf.Save( str, m_editLoad ) )
    if( str.GetLength() )
      AfxMessageBox( tf.GetErrorMessage() );

which saves the contents of the edit box m_edit. As str (the filename) is empty, CTextFile will display a file dialog, and str will contain the selected filename on return.

In the ctor, an extension and the end-of-line marker can be given. The extension defaults to "", e-o-l to "\r\n". The extension will be used to filter files if the standard file dialog is displayed. The somewhat limited support for end-of-line markers include reading from a file to a single CString, and when writing files from a CStringArray.

  • CTextFile::CTextFile( const CString& ext, const CString& eol )

    The ctor. ext can contain the default extension ("txt", for example), and eol the end-of-line marker ("\n", for example). ext defaults to "" and eol to "\r\n".

  • BOOL CTextFile::ReadTextFile( CString& filename, CStringArray& contents )

    Will read the contents of the file filename into the CStringArray contents, one line 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.

  • BOOL CTextFile::ReadTextFile( CString& filename, CString& contents )

    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.

  • BOOL CTextFile::WriteTextFile( CString& filename, const CStringArray& contents )

    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.

  • BOOL CTextFile::WriteTextFile( CString& filename, const CString& contents )

    Writes contents to filename. Will create the file if it doesn't already exist, overwrites 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.

  • BOOL CTextFile::AppendFile( CString& filename, const CString& contents )

    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 end-of-line markers.

  • BOOL CTextFile::AppendFile( CString& filename, const CStringArray& contents )

    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.

  • BOOL CTextFile::Load( CString& filename, CEdit* edit )

    Loads a text file from filename to edit.

    If filename is empty, the standard file dialog will be displayed, and - if OK is selected - filename will contain the selected filename on return. No translation of end-of-line markers will be made.

  • BOOL CTextFile::Load( CString& filename, CListBox* list )

    Loads a text file from filename to list.

    If filename is empty, the standard file dialog will be displayed, and - if OK is selected - filename will contain the selected filename on return.

  • BOOL CTextFile::Save( CString& filename, CEdit* edit )

    Saves the contents of edit to the file filename. The file will be created or overwritten.

    If filename is empty, the standard file dialog will be displayed, and - if OK is selected - filename will contain the selected filename on return. Note that the end-of-line markers from the editbox will be used.

  • BOOL CTextFile::Save( CString& filename, CListBox* list )

    Saves the contents of list to the file filename. The file will be created or overwritten.

    If filename is empty, the standard file dialog will be displayed, and - if OK is selected - filename will contain the selected filename on return.

  • CString CTextFile::GetErrorMessage()

    Retrieves the error message. Should be called after any of the file operations that return FALSE and if the file name is not empty.

Points of Interest

I have to admit it, there are absolutely none. This class was really easy to cobble together, but it has been a great time-saver in its earlier incarnations.

History

  • 2004/03/22 - Initial version.
  • 2005/05/25 - Corrected bug in CTextFile::GetFilename, the creation of the filter string.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here