Simple text save and restore

Ok, I know this is probably the simplest question ever asked and its probably been asked a million times, but here it is:  I've just created a single document mfc appwizard (exe) with the main window descended (right word?) from the CFormView class.  I'm using MS Visual C++ 6.  I haven't added any code yet.  How can I make it so the text in this big edit box is saved to a normal file and can be restored?  Please give me the steps because I'm pretty inexperienced at C++.
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

GabeSmedAuthor Commented:
by 'normal file' I mean a .txt file.
To get the edit control as a variable you can access you will need to use classwizard and click on the member variables tab.  Add a variable to the resource id of the control.

You will then need to do a GetWindowText() on the edit control this will return to you, the contents as a string..

In MFC you can use serialize, however if you are not used to C++ or Visual C++ it can be quite tricky.

So it's time to use the standard C functions....  (We could alternatively use the Windows file operations but I won't explain that here).

        #include <stdio.h>

        FILE *file;
      file = fopen(filename,"w+t");
      if(file == 0)
            printf("Cannot open %s", filename);
      fwrite( thestring, sizeof( char ), strlen(thestring), file );      

I'm sure from this you can work out how to restore the text.....

SetWindowText & fread..

Also be sure to add proper error checking if necessary......


GabeSmedAuthor Commented:
Could you explain the windows functions please?
Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

GabeSmed is NOT using a dialog-based MFC application. He has no edit box from what I can understand.

He's using a typical SDI application and needs to write the contents of his document view as a text file (à-la-Notepad).
GabeSmedAuthor Commented:
yes!.... but I *do* have an edit box.  I'm using CFormView because I need other controls besides the place where you enter text.  Is there a diffent way to do this?
The main thing to worry about is where you store the text of the edit box in the program.

If you are using MFC and ClassWizard, then simply associate a member varaible with the edit box (say m_text).

I will assume that you know the name of the file you want to read to/write from.


In the OnInitDialog for your view, after the call to the base class OnInitDialog, but in code to read your string from the file. eg;

  CStdioFile f;
  if (f.Open("myfile.txt",
  ) {

In you OnOK (you may need to add one of these using the ClassWizard), write it back out:

  CStdioFile f;
  if (f.Open("myfile.txt",
  ) {

That should do it.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
GabeSmedAuthor Commented:
I tried this and it works, but ReadString only brings the first line of the file into the text box.  I assume WriteString does the same.  I tried using Read or ReadHuge, but these take an 'lpBuf' as a parameter, not a CString.  What does this mean, and how can I get the whole file, including line breaks, into the text box?

WriteString will write out the whole thing (multiple lines).

ReadString is (as you said) a line at a time.

If you want to support multiple lines, then just read the lines in one at a time in a loop (doing a readString, and then appending the string with a '\n\' char between).

CString line;
while (f.ReadString(line)) {
  m_test += line;
  m_test += '\n';
GabeSmedAuthor Commented:
Ok... I'm accepting your answer.  Sorry I took so long.  A bunch of problems came up with this solution, but I'll address them in a different question.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.