Solved

How to save text file?

Posted on 2001-06-10
4
241 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
Comment Utility
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
Comment Utility
How to load this text file in CEdit?
0
 
LVL 49

Expert Comment

by:DanRollins
Comment Utility
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
Comment Utility
Next time I'll be sure to look at your grading record to see if you have changed your ways.

-- Dan
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Introduction: Load and Save to file, Document-View interaction inside the SDI. Continuing from the second article about sudoku.   Open the project in visual studio. From the class view select CSudokuDoc and double click to open the header …
Introduction: Ownerdraw of the grid button.  A singleton class implentation and usage. Continuing from the fifth article about sudoku.   Open the project in visual studio. Go to the class view – CGridButton should be visible as a class.  R…
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.
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

771 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now