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

CTextFile: A handy helper

By , 30 Mar 2005
 

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

About the Author

Johan Rosengren
Software Developer (Senior) Abstrakt Mekanik AB
Sweden Sweden
Member
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.

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
QuestionHow to append with a single space before the string append?memberChe Na2 Apr '08 - 22:47 
Good day Johan,
 
It's me again.. Can u please help to show me how to append a string in an exist file with a space before it append.
 
My code seems were not able to solve this. I even tried to have a "" string appended before the words that i want to append but it will be still the same. Below are my codes.
 
CString space = "";
CString s = "123";
 
appsec.AppendFile(recfile,space);
appsec.AppendFile(recfile,s);
 
Results like this
 
15:22:02123
 
It did not have a space between the existed line in the file. Can u please help to advice?
 
Thanks
Zamani
AnswerRe: How to append with a single space before the string append?memberJohan Rosengren4 Apr '08 - 20:02 
space is declared as "", that is, it is no space at all. It should be " ".
GeneralRe: How to append with a single space before the string append?memberChe Na7 Apr '08 - 19:33 
Thanks!! it worked
 
best regards
z
Questionread for 2 different word in a single line in txt filememberChe Na5 Mar '08 - 19:52 
Hello Johan,
Thanks for the fast response on the last question, anyway i have another thing that need help from you.
 
From the classes that you had wrote, is it possible that i can differentiate to 2 different string from two words in a single txt file.
 
For eg,
inside my txt file the are 2 words in a single line
 
eg
stone rock
 
Can i read this line and convert this "stone" and "rock" to 2 differenct CString?
 
CString a = stone CString b = rock.
 
Really appreciate if u can let me know.
 
Thank you very much.
 
Rgds
GeneralRe: read for 2 different word in a single line in txt filememberJohan Rosengren6 Mar '08 - 9:08 
You'll want to call CString::Find to find the space, CString::Left and CString::Mid to split it.
GeneralRe: read for 2 different word in a single line in txt filememberChe Na9 Mar '08 - 21:35 
Thanks for the sharing. It worked.
 
Smile | :)
 
Rgds
CN
GeneralThe string read with other symbol inside... not sure how to remove that...memberChe Na25 Feb '08 - 22:54 
Hello,
BTW, the class is very great. Anyway, i have problem when reading the string from the txt file related to the what it read. Seems like i always have this [] symbol at the end of the string. I don't know how to get the string without this. FOr eg here is the string that i had
20080219140631.bmp[] .... Frown | :(
 
Can u please help if you have any idea?
 
thanks
GeneralRe: The string read with other symbol inside... not sure how to remove that...memberJohan Rosengren26 Feb '08 - 8:17 
This looks like a single little box - if so, this is an unprintable character. It is most likely a carriage return. If so, you'll get rid of it by calling the CString member TrimRight on the string, so: sSomeString.TrimRight();
GeneralRe: The string read with other symbol inside... not sure how to remove that...memberChe Na26 Feb '08 - 21:05 
Hellooo,
yeah, you're correct and it did very well after yr suggestion. Thank you very much for yr great help..
 
thanks Smile | :)
Generalwriting more than 1 line [modified]memberlocoone7 Sep '06 - 13:49 
how?
everything ive tryed has endedup being 1 line i guess it keeps overwriting it
if (m_useall.GetCheck() == 1)
{
chrcount++;
m_wordcount++;
number = chrcount;
file.WriteTextFile(filename, number);
m_currentword = number;
UpdateData(FALSE);
}

im using that in a loop works fine but it only writes on 1 line
i need it to write line after line.
 
and i cant use an array.

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

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