Solved

Open and save to files

Posted on 1998-10-13
6
290 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
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 

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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

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…
What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
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.

746 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now