Solved

How to save text file?

Posted on 2001-06-10
4
247 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
  • 3
4 Comments
 
LVL 49

Accepted Solution

by:
DanRollins earned 100 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
not able to insert into temp table 68 174
isEverywhere  challenge 19 85
Path of Workbook 3 87
x-bar in Google Sheets 2 91
Here is how to use MFC's automatic Radio Button handling in your dialog boxes and forms.  Beginner programmers usually start with a OnClick handler for each radio button and that's just not the right way to go.  MFC has a very cool system for handli…
Introduction: Dialogs (2) modeless dialog and a worker thread.  Handling data shared between threads.  Recursive functions. Continuing from the tenth article about sudoku.   Last article we worked with a modal dialog to help maintain informat…
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 a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

763 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