?
Solved

Open and save to files

Posted on 1998-10-13
6
Medium Priority
?
338 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 800 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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

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.

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…
Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
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.
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
Suggested Courses

752 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