Solved

Closing A File Handle And Initializing A Diaglog Box Value

Posted on 1998-02-08
4
192 Views
Last Modified: 2010-04-10
1.)  I have a dialog based application that opens a file with creatfile.  The file is then wrote to when the user hits the patch button and the application exits.  Now if the user hits the cancel button the program exits and does not write to the file.  Now my question is where should I close the file handle with CloseHandle();.  Right now I am closing the handle in the Patch function, but to me it seems as if the user clicks the cancel button the handle will not be closed.  I am thinking I should put The closehandle in the destructor of the class (of course checking to see if the handle exists before closing it)?

2.)  I have Edit boxes that are integer values.  When the program starts these edit boxes are initalized to 0.  I do not want them to be initalized to anything.  I just want them to be blank upon the programing running.  How can I do this..
0
Comment
Question by:larockd
4 Comments
 
LVL 3

Expert Comment

by:q2guo
ID: 1181221
1.  You can closehandle in the destructor of the class.  But why
can't you open file, do you stuff and close file right after you are done.

2.  You leave the Edit box blank, you will have to initalize it
to '\0'

0
 

Author Comment

by:larockd
ID: 1181222
1.)  The file is opened in a function loadfile, then the user makes changes, and then can hit the PATCH or CANCEL button.  The PATCH button to write the changes to the File or the CANCEL to not write the changes.  Right now I am closing the handle in the patch button, but if the user hits the cancel button the handle is never closed.  I figured to cover this situation I would want to close the handle in the classes destructor?

2.)The Edit box holds an integer value, How can you initalize it to "/0" when it is an integer value?
0
 
LVL 22

Expert Comment

by:nietod
ID: 1181223
Although it is not a good practice, you do not need to close the file at all.  The operating system will close the file when you application terminates.

However, I would do one of two things:

(1) Close the file in a part of the program that is always executed (regardless of what button is hit), such as the end of main() or in a destructor for the dialog or other object.

(2) At the start of the program open the file, read it and close it.  Then if the user hits PATCH, open the file, write it, and close it.  (This is not a the best solution since it would allow multiple programs to edit the file at once.)


0
 
LVL 2

Accepted Solution

by:
AlFa earned 20 total points
ID: 1181224
How an edit box knows what is supposed to be in it?
You can initialize your dialog box with what you want! you just have to be careful!
so if i understand you want to have a dialog box whith retrieve values and never init them! so just do it that way in the DoDataExchange use the m_bSaveAndValidate member of the cdataexchange.

What about your file?
I think it's a good idea to close it in the destructor. If you do not like that try in  WM_DESTROY message handler.
0

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Suggested Solutions

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. …
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
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 be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

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