Solved

Open and save to files

Posted on 1998-10-13
6
319 Views
Last Modified: 2008-03-03
I need help on how to save data to a file and how to retrieve the data from the file.

I'm using V++ 5.0 and am in Dialog mode.

I used the dialog editor to add an EDIT box and two BUTTONS.
The first button I have I call SAVE and wrote this code for it, which I got as an example from a book.

void CFile_openDlg::OnWriteButton()
{
#ifndef _MAC
      CFileDialog dlg( FALSE,_T("AVI"),_T("*.AVI"),
                       OFN_HIDEREADONLY|OFN_OVERWRITEPROMPT,
                       _T("Animation (*.AVI)|*.AVI|"));
#else
      CFileDialog dlg( FALSE,NULL,NULL,
                       OFN_HIDEREADONLY|OFN_OVERWRITEPROMPT,
                       _T("Animation|AVI |"));
#endif
      if( dlg.DoModal()==IDOK )
      {
            

      }
}

The second button I have I call OPEN and wrote this code for it, which I also got as an example from a book.

void CFile_openDlg::OnOpenButton()
{
 #ifndef _MAC
      CFileDialog dlg( TRUE,_T("AVI"),_T("*.AVI"),
                       OFN_HIDEREADONLY|OFN_OVERWRITEPROMPT,
                       _T("Animation (*.AVI)|*.AVI|"));
#else
      CFileDialog dlg( TRUE,NULL,NULL,
                       OFN_HIDEREADONLY|OFN_OVERWRITEPROMPT,
                       _T("Animation|AVI |"));
#endif
      if( dlg.DoModal()==IDOK )
      {

      }
      
}

My question is I would like to enter a number into the EDIT box and then be able to save the variable to a file. And then retrieve it using OPEN.

What do I need to add to this code to do this?

Thanks
Randy
0
Comment
Question by:randyg
  • 3
  • 3
6 Comments
 
LVL 86

Accepted Solution

by:
jkr earned 200 total points
ID: 1174973
At first, use the ClassWizard to add a member variable to your dialog class, specifying the Edit box' control ID (usually something like 'IDC_EDIT1'), let's say a 'CString m_strEdit;'. Then, add a call to 'UpdateData( TRUE);' to the beginning of your save function, which will cause the m_strEdit to be filled with the text (or number) in the Edit box. To write this to a file, use
UpdateData( TRUE);
if( dlg.DoModal()==IDOK )
{
CStdioFile cFile;

        try
        {
            if  (   !cFile.Open   (   dlg.GetPathName(),    
                                        CFile::modeWrite | CFile::typeText
                                    )
                )   return;

            cFile.WriteString ( m_strEdit);

        }
        catch   (   CFileException* e)
        {
            CString strErr;
            strErr.Format   (   "ERROR processing '%s', reason ==%d\n",
                                e->m_strFileName,
                                e->m_lOsError
                            );

            AfxMessageBox   (   strErr);

            e->Delete   ();

        }

  cFile.Close ();
}

To read from a file, use
if( dlg.DoModal()==IDOK )
{
CStdioFile cFile;

        try
        {
            if  (   !cFile.Open   (   dlg.GetPathName(),    
                                        CFile::modeRead | CFile::typeText
                                    )
                )   return;

            cFile.ReadString ( m_strEdit);

        }
        catch   (   CFileException* e)
        {
            CString strErr;
            strErr.Format   (   "ERROR processing '%s', reason ==%d\n",
                                e->m_strFileName,
                                e->m_lOsError
                            );

            AfxMessageBox   (   strErr);

            e->Delete   ();

        }

  cFile.Close ();
}
 
UpdateData( TRUE);

0
 

Author Comment

by:randyg
ID: 1174974
Thanks for the response. I finally got a chance to work on it again and this is what happened.

If I added this to the program it wouldn't write. It seems to run like it is but it never creates the file or writes data to an exsisting file. Do you have any other suggestions?

Thanks
Randy

UpdateData( TRUE);
    if( dlg.DoModal()==IDOK )
    {
    CStdioFile cFile;

            try
            {
                if  (   !cFile.Open   (   dlg.GetPathName(),    
                                            CFile::modeWrite | CFile::typeText
                                        )
                    )   return;

                cFile.WriteString ( m_strEdit);

            }


      cFile.Close ();
    }
0
 
LVL 86

Expert Comment

by:jkr
ID: 1174975
Well, does it run to the line where 'WriteString()' is called or doesn't it even reach it? (BTW: Due to laziness, i just copied the code out of an existing project that DOES work ;-)
0
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 

Author Comment

by:randyg
ID: 1174976
Thanks for the response. I finally got a chance to work on it again and this is what happened.

If I added this to the program it wouldn't write. It seems to run like it is but it never creates the file or writes data to an exsisting file. Do you have any other suggestions?

Thanks
Randy

UpdateData( TRUE);
    if( dlg.DoModal()==IDOK )
    {
    CStdioFile cFile;

            try
            {
                if  (   !cFile.Open   (   dlg.GetPathName(),    
                                            CFile::modeWrite | CFile::typeText
                                        )
                    )   return;

                cFile.WriteString ( m_strEdit);

            }


      cFile.Close ();
    }
0
 
LVL 86

Expert Comment

by:jkr
ID: 1174977
Consider trying somthing like this:
    if( dlg.DoModal()==IDOK )
    {
    CStdioFile cFile;

            try
            {
                if  (   !cFile.Open   (   "c:\\test.txt",     
                                            CFile::modeWrite | CFile::typeText
                                        )
                    )   return;

                cFile.WriteString ( "test");

            }


      cFile.Close ();
    }

If this does work, we'll continue examinig what could have gone wrong...
0
 

Author Comment

by:randyg
ID: 1174978
That did it!!!!

Thanks for your help!!!!

Randy
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

830 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