?
Solved

Checking Handles

Posted on 1998-02-08
5
Medium Priority
?
182 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
[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
  • 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

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

Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
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 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 learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
Suggested Courses

764 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