Click here to Skip to main content
Click here to Skip to main content

CTextFile: A handy helper

, 30 Mar 2005 Public Domain
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.

Comments and Discussions

 
GeneralRe: How to read the file fully? PinmemberJohan Rosengren29-Oct-04 6:09 
GeneralRe: How to read the file fully? PinmemberThiagarajan Rajendran31-Oct-04 16:57 
GeneralRe: How to read the file fully? PinmemberJohan Rosengren1-Nov-04 5:55 
Generaltexfile for pocket pc Pinmemberriki_risnandar1-Oct-04 11:04 
GeneralRe: texfile for pocket pc PinmemberJohan Rosengren1-Oct-04 20:01 
GeneralRe: texfile for pocket pc Pinmemberriki_risnandar12-Oct-04 9:27 
GeneralRe: texfile for pocket pc PinmemberJohan Rosengren12-Oct-04 21:59 
GeneralRe: texfile for pocket pc PinmemberEdipo22-Nov-04 23:39 
I'm trying too use it for Pocket PC but I have the next problem:
What I'm doing bad?
 
I did: Project --> Add to Project --> Files
and I selected the two files: (textfile.h & textfile.cpp) that I had copied to my project's directory!
 
--------------------Configuration: testingsqlite - Win32 (WCE ARMV4) Release--------------------
Compiling...
TextFile.cpp
c:\archivos de programa\microsoft visual studio\myprojects\testingsqlite\textfile.h(10) : error C2629: unexpected 'class CTextFile ('
c:\archivos de programa\microsoft visual studio\myprojects\testingsqlite\textfile.h(10) : error C2238: unexpected token(s) preceding ';'
c:\archivos de programa\microsoft visual studio\myprojects\testingsqlite\textfile.h(16) : error C2061: syntax error : identifier 'CString'
c:\archivos de programa\microsoft visual studio\myprojects\testingsqlite\textfile.h(17) : error C2061: syntax error : identifier 'CString'
c:\archivos de programa\microsoft visual studio\myprojects\testingsqlite\textfile.h(17) : error C2535: 'int __cdecl CTextFile::ReadTextFile(void)' : member function already defined or declared
c:\archivos de programa\microsoft visual studio\myprojects\testingsqlite\textfile.h(16) : see declaration of 'ReadTextFile'
c:\archivos de programa\microsoft visual studio\myprojects\testingsqlite\textfile.h(19) : error C2061: syntax error : identifier 'CString'
c:\archivos de programa\microsoft visual studio\myprojects\testingsqlite\textfile.h(20) : error C2061: syntax error : identifier 'CString'
c:\archivos de programa\microsoft visual studio\myprojects\testingsqlite\textfile.h(20) : error C2535: 'int __cdecl CTextFile::WriteTextFile(void)' : member function already defined or declared
c:\archivos de programa\microsoft visual studio\myprojects\testingsqlite\textfile.h(19) : see declaration of 'WriteTextFile'
c:\archivos de programa\microsoft visual studio\myprojects\testingsqlite\textfile.h(22) : error C2061: syntax error : identifier 'CString'
c:\archivos de programa\microsoft visual studio\myprojects\testingsqlite\textfile.h(23) : error C2061: syntax error : identifier 'CString'
c:\archivos de programa\microsoft visual studio\myprojects\testingsqlite\textfile.h(23) : error C2535: 'int __cdecl CTextFile::AppendFile(void)' : member function already defined or declared
c:\archivos de programa\microsoft visual studio\myprojects\testingsqlite\textfile.h(22) : see declaration of 'AppendFile'
c:\archivos de programa\microsoft visual studio\myprojects\testingsqlite\textfile.h(26) : error C2061: syntax error : identifier 'CString'
c:\archivos de programa\microsoft visual studio\myprojects\testingsqlite\textfile.h(27) : error C2061: syntax error : identifier 'CString'
c:\archivos de programa\microsoft visual studio\myprojects\testingsqlite\textfile.h(27) : error C2535: 'int __cdecl CTextFile::Load(void)' : member function already defined or declared
c:\archivos de programa\microsoft visual studio\myprojects\testingsqlite\textfile.h(26) : see declaration of 'Load'
c:\archivos de programa\microsoft visual studio\myprojects\testingsqlite\textfile.h(28) : error C2061: syntax error : identifier 'CString'
c:\archivos de programa\microsoft visual studio\myprojects\testingsqlite\textfile.h(29) : error C2061: syntax error : identifier 'CString'
c:\archivos de programa\microsoft visual studio\myprojects\testingsqlite\textfile.h(29) : error C2535: 'int __cdecl CTextFile::Save(void)' : member function already defined or declared
c:\archivos de programa\microsoft visual studio\myprojects\testingsqlite\textfile.h(28) : see declaration of 'Save'
c:\archivos de programa\microsoft visual studio\myprojects\testingsqlite\textfile.h(32) : error C2146: syntax error : missing ';' before identifier 'GetErrorMessage'
c:\archivos de programa\microsoft visual studio\myprojects\testingsqlite\textfile.h(32) : error C2501: 'CString' : missing storage-class or type specifiers
c:\archivos de programa\microsoft visual studio\myprojects\testingsqlite\textfile.h(36) : error C2061: syntax error : identifier 'CString'
c:\archivos de programa\microsoft visual studio\myprojects\testingsqlite\textfile.h(37) : error C2146: syntax error : missing ';' before identifier 'GetExtension'
c:\archivos de programa\microsoft visual studio\myprojects\testingsqlite\textfile.h(37) : error C2501: 'CString' : missing storage-class or type specifiers
c:\archivos de programa\microsoft visual studio\myprojects\testingsqlite\textfile.h(41) : error C2146: syntax error : missing ';' before identifier 'm_error'
c:\archivos de programa\microsoft visual studio\myprojects\testingsqlite\textfile.h(41) : error C2501: 'CString' : missing storage-class or type specifiers
c:\archivos de programa\microsoft visual studio\myprojects\testingsqlite\textfile.h(41) : error C2501: 'm_error' : missing storage-class or type specifiers
c:\archivos de programa\microsoft visual studio\myprojects\testingsqlite\textfile.h(42) : error C2146: syntax error : missing ';' before identifier 'm_extension'
c:\archivos de programa\microsoft visual studio\myprojects\testingsqlite\textfile.h(42) : error C2501: 'CString' : missing storage-class or type specifiers
c:\archivos de programa\microsoft visual studio\myprojects\testingsqlite\textfile.h(42) : error C2501: 'm_extension' : missing storage-class or type specifiers
c:\archivos de programa\microsoft visual studio\myprojects\testingsqlite\textfile.h(43) : error C2146: syntax error : missing ';' before identifier 'm_eol'
c:\archivos de programa\microsoft visual studio\myprojects\testingsqlite\textfile.h(43) : error C2501: 'CString' : missing storage-class or type specifiers
c:\archivos de programa\microsoft visual studio\myprojects\testingsqlite\textfile.h(43) : error C2501: 'm_eol' : missing storage-class or type specifiers
c:\archivos de programa\microsoft visual studio\myprojects\testingsqlite\textfile.h(46) : error C2061: syntax error : identifier 'CWnd'
c:\archivos de programa\microsoft visual studio\myprojects\testingsqlite\textfile.cpp(30) : error C2143: syntax error : missing ',' before '&'
c:\archivos de programa\microsoft visual studio\myprojects\testingsqlite\textfile.cpp(30) : error C2059: syntax error : '&'
c:\archivos de programa\microsoft visual studio\myprojects\testingsqlite\textfile.cpp(44) : error C2511: 'CTextFile::CTextFile' : overloaded member function 'void (const int)' not found in 'CTextFile'
c:\archivos de programa\microsoft visual studio\myprojects\testingsqlite\textfile.h(5) : see declaration of 'CTextFile'
c:\archivos de programa\microsoft visual studio\myprojects\testingsqlite\textfile.cpp(67) : error C2065: 'CString' : undeclared identifier
c:\archivos de programa\microsoft visual studio\myprojects\testingsqlite\textfile.cpp(67) : error C2065: 'filename' : undeclared identifier
c:\archivos de programa\microsoft visual studio\myprojects\testingsqlite\textfile.cpp(67) : error C2065: 'CStringArray' : undeclared identifier
c:\archivos de programa\microsoft visual studio\myprojects\testingsqlite\textfile.cpp(67) : error C2065: 'contents' : undeclared identifier
c:\archivos de programa\microsoft visual studio\myprojects\testingsqlite\textfile.cpp(87) : error C2448: '' : function-style initializer appears to be a function definition
c:\archivos de programa\microsoft visual studio\myprojects\testingsqlite\textfile.cpp(146) : error C2448: '' : function-style initializer appears to be a function definition
c:\archivos de programa\microsoft visual studio\myprojects\testingsqlite\textfile.cpp(190) : error C2059: syntax error : 'const'
c:\archivos de programa\microsoft visual studio\myprojects\testingsqlite\textfile.cpp(211) : error C2143: syntax error : missing ';' before '{'
c:\archivos de programa\microsoft visual studio\myprojects\testingsqlite\textfile.cpp(211) : error C2447: missing function header (old-style formal list?)
c:\archivos de programa\microsoft visual studio\myprojects\testingsqlite\textfile.cpp(252) : error C2059: syntax error : 'const'
c:\archivos de programa\microsoft visual studio\myprojects\testingsqlite\textfile.cpp(271) : error C2143: syntax error : missing ';' before '{'
c:\archivos de programa\microsoft visual studio\myprojects\testingsqlite\textfile.cpp(271) : error C2447: missing function header (old-style formal list?)
c:\archivos de programa\microsoft visual studio\myprojects\testingsqlite\textfile.cpp(309) : error C2059: syntax error : 'const'
c:\archivos de programa\microsoft visual studio\myprojects\testingsqlite\textfile.cpp(327) : error C2143: syntax error : missing ';' before '{'
c:\archivos de programa\microsoft visual studio\myprojects\testingsqlite\textfile.cpp(327) : error C2447: missing function header (old-style formal list?)
c:\archivos de programa\microsoft visual studio\myprojects\testingsqlite\textfile.cpp(363) : error C2059: syntax error : 'const'
c:\archivos de programa\microsoft visual studio\myprojects\testingsqlite\textfile.cpp(381) : error C2143: syntax error : missing ';' before '{'
c:\archivos de programa\microsoft visual studio\myprojects\testingsqlite\textfile.cpp(381) : error C2447: missing function header (old-style formal list?)
c:\archivos de programa\microsoft visual studio\myprojects\testingsqlite\textfile.cpp(425) : error C2065: 'CEdit' : undeclared identifier
c:\archivos de programa\microsoft visual studio\myprojects\testingsqlite\textfile.cpp(425) : error C2065: 'edit' : undeclared identifier
c:\archivos de programa\microsoft visual studio\myprojects\testingsqlite\textfile.cpp(444) : error C2448: '' : function-style initializer appears to be a function definition
c:\archivos de programa\microsoft visual studio\myprojects\testingsqlite\textfile.cpp(464) : error C2065: 'CListBox' : undeclared identifier
c:\archivos de programa\microsoft visual studio\myprojects\testingsqlite\textfile.cpp(464) : error C2065: 'list' : undeclared identifier
c:\archivos de programa\microsoft visual studio\myprojects\testingsqlite\textfile.cpp(482) : error C2448: '' : function-style initializer appears to be a function definition
c:\archivos de programa\microsoft visual studio\myprojects\testingsqlite\textfile.cpp(532) : error C2448: '' : function-style initializer appears to be a function definition
c:\archivos de programa\microsoft visual studio\myprojects\testingsqlite\textfile.cpp(574) : error C2448: '' : function-style initializer appears to be a function definition
c:\archivos de programa\microsoft visual studio\myprojects\testingsqlite\textfile.cpp(607) : error C2143: syntax error : missing ';' before 'tag::id'
c:\archivos de programa\microsoft visual studio\myprojects\testingsqlite\textfile.cpp(607) : error C2501: 'CString' : missing storage-class or type specifiers
c:\archivos de programa\microsoft visual studio\myprojects\testingsqlite\textfile.cpp(607) : fatal error C1004: unexpected end of file found
Error executing clarm.exe.
 
TextFile.obj - 64 error(s), 0 warning(s)

 
Aaron Planell
aaronpl@ya.com
GeneralRe: texfile for pocket pc PinmemberJohan Rosengren23-Nov-04 7:43 
GeneralRe: texfile for pocket pc Pinmemberriki_risnandar23-Nov-04 10:16 
GeneralRe: texfile for pocket pc PinmemberEdipo23-Nov-04 12:12 
GeneralRe: texfile for pocket pc PinmemberJohan Rosengren25-Nov-04 5:13 
GeneralRe: texfile for pocket pc Pinmemberriki_risnandar25-Nov-04 12:34 
GeneralRe: texfile for pocket pc PinmemberEdipo26-Nov-04 2:48 
GeneralRe: texfile for pocket pc PinmemberJohan Rosengren26-Nov-04 7:05 
GeneralRe: texfile for pocket pc PinmemberJohan Rosengren25-Nov-04 5:10 
GeneralReally Fantastic Pinmemberbaoiph9-Jun-04 2:28 
QuestionHow to check whether the file exist or not?? PinmemberJigar Mehta4-Jun-04 20:20 
AnswerRe: How to check whether the file exist or not?? PinmemberJohan Rosengren4-Jun-04 20:40 
AnswerRe: How to check whether the file exist or not?? Pinmemberyarp30-Mar-05 17:35 
General! Good, but it's a wheel PinmemberSynetech1-May-04 15:02 
GeneralRe: ! Good, but it's a wheel PinmemberJohan Rosengren1-May-04 23:34 
GeneralRe: ! Good, but it's a wheel PinmemberSynetech2-May-04 13:38 
GeneralRe: ! Good, but it's a wheel PinmemberJohan Rosengren2-May-04 21:08 
QuestionHow use this code for UNICODE text files ? PinmemberCDU31-Mar-04 21:03 

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 | Mobile
Web04 | 2.8.141022.2 | Last Updated 30 Mar 2005
Article Copyright 2004 by Johan Rosengren
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid