Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

How to save text file?

Posted on 2001-06-10
4
Medium Priority
?
251 Views
Last Modified: 2013-11-20
I have a edit box in Dialog.
I want to save text in edit box (*.txt).
Using CFileDialog.
0
Comment
Question by:pbtdanh
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
4 Comments
 
LVL 49

Accepted Solution

by:
DanRollins earned 300 total points
ID: 6174854
Try this:
//--------- handy utility fn
BOOL StrToTextFile( LPCSTR szFile, CString& sText )
{
    CFile cFile;
    BOOL fRet= cFile.Open( szFile, CFile::modeCreate | CFile::modeWrite );
    if ( !fRet ) {
        return( FALSE ); // error occured
    }
    try {
        cFile.Write( (LPCSTR)sText, sText.GetLength() );
    }
    catch ( CFileException* pEx ) {
        pEx->ReportError();
       return( FALSE );
    }
    cFile.Close();
   return( TRUE );
}

//----------------------------- example of usage
void CMyDlg::OnButton1()
{
    // make it a "Save As..." dlg and provide a default filename
    CFileDialog dlg( FALSE, "TXT", "NewFile.txt" );  
    int nRet= dlg.DoModal();
    if (nRet == IDOK ) {
       CString sFile= dlg.m_ofn.lpstrFile;
   
       // Now get the text of the EditBox
       CString sTextToSave;
       m_ctlEdit.GetWindowText( sTextToSave );
       // or GetDlgItem(IDC_EDIT1)->GetWindowText(...)    

       // Now save the text to the file
       BOOL fRet=  StrToTextFile( sFile, sTextToSave );
       // TRUE mean no error, else add a msgbox here
    }
}

If you need any part of this explained, just ask!  I'll provide whatever details you need.

-- Dan
0
 

Author Comment

by:pbtdanh
ID: 6178172
How to load this text file in CEdit?
0
 
LVL 49

Expert Comment

by:DanRollins
ID: 6178564
Hi pbtdanh,
As an expert here at EE, I am striving for a "4.0 grade point average."  When you accept my comment as a "Good" answer, it lowers my average.  Please answer these questions, to help me understand how to improve:

1) Did the comment I posted help you to solve your problem?

2) Obviously my comment was less than a perfect solution in your eyes.  What could I have added to make it 100% perfect?

3) Do you understand that if the comment does not fully solve your problem, that you have the option -- in fact, the responsibility -- to respond to clarify the question or add some details?

4) Do you understand that it costs you no more to give an A than it does to give a B or a C, but that the expert recieves more points for the better grade?

5) Have you read the information at
http://www.experts-exchange.com/jsp/cmtyQuestAnswer.jsp#3
and do you understand it?

6) Is there anything I said that offended you in any way?

-- Dan
0
 
LVL 49

Expert Comment

by:DanRollins
ID: 6307073
Next time I'll be sure to look at your grading record to see if you have changed your ways.

-- Dan
0

Featured Post

Will your db performance match your db growth?

In Percona’s white paper “Performance at Scale: Keeping Your Database on Its Toes,” we take a high-level approach to what you need to think about when planning for database scalability.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
In this post we will learn different types of Android Layout and some basics of an Android App.
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
Suggested Courses

661 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question