Solved

How to save text file?

Posted on 2001-06-10
4
249 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 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

Ready to get started with anonymous questions?

It's easy! Check out this step-by-step guide for asking an anonymous question on Experts Exchange.

Question has a verified solution.

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

Introduction: Hints for the grid button.  Nested classes, templated collections.  Squash that darned bug! Continuing from the sixth article about sudoku.   Open the project in visual studio. First we will finish with the SUD_SETVALUE messa…
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.
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.
There's a multitude of different network monitoring solutions out there, and you're probably wondering what makes NetCrunch so special. It's completely agentless, but does let you create an agent, if you desire. It offers powerful scalability …

624 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