Solved

How to save text file?

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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
iSeries FTP Exit Program 8 137
Creating an alias to a directory so I can change the directory 'contents' by changing the alias 5 86
NotAlone Challenge 20 84
EvenOdd challenge 10 109
This is to be the first in a series of articles demonstrating the development of a complete windows based application using the MFC classes.  I’ll try to keep each article focused on one (or a couple) of the tasks that one may meet.   Introductio…
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 Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
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.

831 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