Solved

Checking Handles

Posted on 1998-02-08
5
175 Views
Last Modified: 2010-04-10
I have a dialog based application that has a menu attached to it, with an option to load a data file.  Now if the user selects the menu item "load" a file is then loaded.  Now if a user then selects the menu item "Load" again another file is loaded.

Now my question is about file handles.  Now because the user loaded two different files is there two file handle's open?  This might need a bit more explaining.  Now, since the first file was never closed it is still open, and now that I opened another file it is open...  I imagine over a period of time this is bad?  Is this true?  So how would I check before I let the user load another file that the first file needs to be closed?
I imagine that I would have to check the handle somehow.  Do I even have to worry about this?
0
Comment
Question by:larockd
  • 2
  • 2
5 Comments
 
LVL 3

Expert Comment

by:q2guo
ID: 1181479
Opening more than one file in a program is common.  There is
nothing to worry about, both files have their own handle, they
won't interfere.  But, whether you program needs to have two files open at the same file really depends on what you want to do.

0
 

Author Comment

by:larockd
ID: 1181480
I guess my question might have been a bit vague.  The application  only needs to work with one file at a time.  Therefor, I only have one file handle.  When the user loads another file the old file handle becomes a file handle for the new file, thus leading the other file open and no way to close it because there is now no handle to it.  Over a period of time this (I would imagine) will lead to problems if the users opens a lot of files...  So it leads me to my original question as stated above how should I deal with this...  
0
 
LVL 32

Accepted Solution

by:
jhance earned 0 total points
ID: 1181481
You are correct, this is bad and over time (provided the user did this enough times), the system would run out of resources.  There are a couple of things you might do:

1) One a user has opened a file, set a flag, disable the "Open File" menu or button until the file has been closed.

2) If the user chooses open file when one is already open, close the open one and then open the new one.  How to do this?  You didn't specify your OS but here's how you might do it:


/* Main Program */
main()
...
FILE *fp = NULL;
...
fp = OpenFile(fp, openFileName);

...

/* Open the file function */
FILE * OpenFile(FILE *fp, char *fn)
{
  if(fp != NULL){
    /* File is open, close and then open a new one */
    fclose(fp);
  }
 
  return(fopen(fn, "r"));
}
0
 

Author Comment

by:larockd
ID: 1181482
Jhance,
  I am using Visual C++ 5.0, and the application is for Win 95.  I am opening the file CreateFile, and closing it with CloseHandle.  Is it the same way as above?
0
 
LVL 32

Expert Comment

by:jhance
ID: 1181483
Basically, it's the same.  CreateFile returns a HANDLE instead of fopen's FILE *, but you can use the same logic.

HANDLE OpenFile(HANDLE hFp, char *fn)
{
  if(hFp != NULL){
    CloseHandle(hFp);
  }

  return(CreateFile(...));
}
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

Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
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.

747 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

18 Experts available now in Live!

Get 1:1 Help Now