Solved

Open and save to files

Posted on 1998-10-13
6
330 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
[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
  • 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
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.

695 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