Click here to Skip to main content
11,577,240 members (62,759 online)
Click here to Skip to main content

CTextFile: A handy helper

, 30 Mar 2005 Public Domain 146.3K 2.5K 61
Rate this:
Please Sign up or sign in to vote.
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, along with any associated source code and files, is licensed under A Public Domain dedication

Share

About the Author

Johan Rosengren
Software Developer (Senior) Abstrakt Mekanik AB
Sweden Sweden
45 years old, married, three kids.

Started with computers more than 20 years ago on a CBM-64.

Read Theoretical Philosophy at the University of Lund.

Working as a C++ consultant developer.

Science-fiction freak. Enjoy vintage punkrock.

You may also be interested in...

Comments and Discussions

 
QuestionHow to append with a single space before the string append? Pin
Che Na2-Apr-08 22:47
memberChe Na2-Apr-08 22:47 
AnswerRe: How to append with a single space before the string append? Pin
Johan Rosengren4-Apr-08 20:02
memberJohan Rosengren4-Apr-08 20:02 
GeneralRe: How to append with a single space before the string append? Pin
Che Na7-Apr-08 19:33
memberChe Na7-Apr-08 19:33 
Questionread for 2 different word in a single line in txt file Pin
Che Na5-Mar-08 19:52
memberChe Na5-Mar-08 19:52 
GeneralRe: read for 2 different word in a single line in txt file Pin
Johan Rosengren6-Mar-08 9:08
memberJohan Rosengren6-Mar-08 9:08 
GeneralRe: read for 2 different word in a single line in txt file Pin
Che Na9-Mar-08 21:35
memberChe Na9-Mar-08 21:35 
GeneralThe string read with other symbol inside... not sure how to remove that... Pin
Che Na25-Feb-08 22:54
memberChe Na25-Feb-08 22:54 
GeneralRe: The string read with other symbol inside... not sure how to remove that... Pin
Johan Rosengren26-Feb-08 8:17
memberJohan Rosengren26-Feb-08 8:17 
GeneralRe: The string read with other symbol inside... not sure how to remove that... Pin
Che Na26-Feb-08 21:05
memberChe Na26-Feb-08 21:05 
Generalwriting more than 1 line [modified] Pin
locoone7-Sep-06 13:49
memberlocoone7-Sep-06 13:49 
GeneralRe: writing more than 1 line Pin
Joan Murt6-Feb-07 20:35
memberJoan Murt6-Feb-07 20:35 
QuestionWriting a large CString to CFile Pin
na.nu8-Apr-06 18:06
memberna.nu8-Apr-06 18:06 
AnswerRe: Writing a large CString to CFile Pin
Johan Rosengren8-Apr-06 22:57
memberJohan Rosengren8-Apr-06 22:57 
QuestionRe: Writing a large CString to CFile Pin
na.nu11-Apr-06 17:54
memberna.nu11-Apr-06 17:54 
AnswerRe: Writing a large CString to CFile Pin
Johan Rosengren12-Apr-06 5:56
memberJohan Rosengren12-Apr-06 5:56 
GeneralRe: Writing a large CString to CFile Pin
na.nu12-Apr-06 21:36
memberna.nu12-Apr-06 21:36 
GeneralRe: Writing a large CString to CFile Pin
Johan Rosengren13-Apr-06 6:51
memberJohan Rosengren13-Apr-06 6:51 
GeneralAdding .bak feature... Pin
Hugh S. Myers27-Mar-06 15:22
memberHugh S. Myers27-Mar-06 15:22 
GeneralRe: Adding .bak feature... Pin
Johan Rosengren28-Mar-06 4:56
memberJohan Rosengren28-Mar-06 4:56 
GeneralRe: Adding .bak feature... Pin
Hugh S. Myers30-Mar-06 5:02
memberHugh S. Myers30-Mar-06 5:02 
GeneralThanks! Pin
sfdougl16-Nov-05 1:02
membersfdougl16-Nov-05 1:02 
GeneralRe: Thanks! Pin
Johan Rosengren16-Nov-05 7:18
memberJohan Rosengren16-Nov-05 7:18 
GeneralRe: Thanks! Pin
sfdougl16-Nov-05 18:15
membersfdougl16-Nov-05 18:15 
GeneralRe: Thanks! Pin
Johan Rosengren17-Nov-05 7:23
memberJohan Rosengren17-Nov-05 7:23 
GeneralRe: Thanks! Pin
sfdougl17-Nov-05 10:54
membersfdougl17-Nov-05 10:54 
QuestionHow to read the file fully? Pin
Meenakshi Chockalingam14-Oct-04 21:36
memberMeenakshi Chockalingam14-Oct-04 21:36 
AnswerRe: How to read the file fully? Pin
Johan Rosengren14-Oct-04 21:51
memberJohan Rosengren14-Oct-04 21:51 
GeneralRe: How to read the file fully? Pin
Meenakshi Chockalingam14-Oct-04 23:12
memberMeenakshi Chockalingam14-Oct-04 23:12 
GeneralRe: How to read the file fully? Pin
Meenakshi Chockalingam14-Oct-04 23:22
memberMeenakshi Chockalingam14-Oct-04 23:22 
GeneralRe: How to read the file fully? Pin
Johan Rosengren14-Oct-04 23:54
memberJohan Rosengren14-Oct-04 23:54 
AnswerRe: How to read the file fully? Pin
Thiagarajan Rajendran29-Oct-04 0:20
memberThiagarajan Rajendran29-Oct-04 0:20 
GeneralRe: How to read the file fully? Pin
Johan Rosengren29-Oct-04 6:09
memberJohan Rosengren29-Oct-04 6:09 
GeneralRe: How to read the file fully? Pin
Thiagarajan Rajendran31-Oct-04 16:57
memberThiagarajan Rajendran31-Oct-04 16:57 
GeneralRe: How to read the file fully? Pin
Johan Rosengren1-Nov-04 5:55
memberJohan Rosengren1-Nov-04 5:55 
Generaltexfile for pocket pc Pin
riki_risnandar1-Oct-04 11:04
memberriki_risnandar1-Oct-04 11:04 
GeneralRe: texfile for pocket pc Pin
Johan Rosengren1-Oct-04 20:01
memberJohan Rosengren1-Oct-04 20:01 
GeneralRe: texfile for pocket pc Pin
riki_risnandar12-Oct-04 9:27
memberriki_risnandar12-Oct-04 9:27 
GeneralRe: texfile for pocket pc Pin
Johan Rosengren12-Oct-04 21:59
memberJohan Rosengren12-Oct-04 21:59 
riki_risnandar wrote:
Thank you for your guide and your usefull class

And thank you for your feedback!
GeneralRe: texfile for pocket pc Pin
Edipo22-Nov-04 23:39
memberEdipo22-Nov-04 23:39 
GeneralRe: texfile for pocket pc Pin
Johan Rosengren23-Nov-04 7:43
memberJohan Rosengren23-Nov-04 7:43 
GeneralRe: texfile for pocket pc Pin
riki_risnandar23-Nov-04 10:16
memberriki_risnandar23-Nov-04 10:16 
GeneralRe: texfile for pocket pc Pin
Edipo23-Nov-04 12:12
memberEdipo23-Nov-04 12:12 
GeneralRe: texfile for pocket pc Pin
Johan Rosengren25-Nov-04 5:13
memberJohan Rosengren25-Nov-04 5:13 
GeneralRe: texfile for pocket pc Pin
riki_risnandar25-Nov-04 12:34
memberriki_risnandar25-Nov-04 12:34 
GeneralRe: texfile for pocket pc Pin
Edipo26-Nov-04 2:48
memberEdipo26-Nov-04 2:48 
GeneralRe: texfile for pocket pc Pin
Johan Rosengren26-Nov-04 7:05
memberJohan Rosengren26-Nov-04 7:05 
GeneralRe: texfile for pocket pc Pin
Johan Rosengren25-Nov-04 5:10
memberJohan Rosengren25-Nov-04 5:10 
GeneralReally Fantastic Pin
baoiph9-Jun-04 2:28
memberbaoiph9-Jun-04 2:28 
QuestionHow to check whether the file exist or not?? Pin
Jigar Mehta4-Jun-04 20:20
memberJigar Mehta4-Jun-04 20:20 
AnswerRe: How to check whether the file exist or not?? Pin
Johan Rosengren4-Jun-04 20:40
memberJohan Rosengren4-Jun-04 20:40 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.150603.1 | Last Updated 30 Mar 2005
Article Copyright 2004 by Johan Rosengren
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid