[Webinar] Streamline your web hosting managementRegister Today

x
?
Solved

Simple text save and restore

Posted on 2000-02-11
9
Medium Priority
?
234 Views
Last Modified: 2010-05-18
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++.
0
Comment
Question by:GabeSmed
9 Comments
 

Author Comment

by:GabeSmed
ID: 2513413
by 'normal file' I mean a .txt file.
0
 
LVL 1

Expert Comment

by:cwgues
ID: 2513626
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..

Filesaving
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);
            return(false);
      }
      fwrite( thestring, sizeof( char ), strlen(thestring), file );      
        fclose(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......

HTH
   

0
 

Author Comment

by:GabeSmed
ID: 2513706
Could you explain the windows functions please?
0
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
LVL 2

Expert Comment

by:bbousquet
ID: 2513721
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).
0
 

Author Comment

by:GabeSmed
ID: 2513746
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?
0
 
LVL 10

Accepted Solution

by:
RONSLOW earned 100 total points
ID: 2549189
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.

OK.

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",
    CFile::modeRead)
  ) {
    f.ReadString(m_text);
  }

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",
    CFile::modeCreate|CFile::modeWrite)
  ) {
    f.WriteString(m_text);
    f.WriteString("\n");
  }

That should do it.
0
 

Author Comment

by:GabeSmed
ID: 2554627
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?

Thanks.
0
 
LVL 10

Expert Comment

by:RONSLOW
ID: 2556182
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).
eg.

m_test.Empty();
CString line;
while (f.ReadString(line)) {
  m_test += line;
  m_test += '\n';
}
0
 

Author Comment

by:GabeSmed
ID: 2562914
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.
0

Featured Post

The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

Question has a verified solution.

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

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
Suggested Courses

591 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