Solved

Open and save to files

Posted on 1998-10-13
6
310 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
Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

 

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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
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…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
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.

831 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