Solved

Checking Handles

Posted on 1998-02-08
5
179 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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

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

Suggested Solutions

Title # Comments Views Activity
Best book to learn C++ 4 87
c++ reading data from file into two dimensional array 3 129
Beginner to Unreal Engine 4 5 104
Why  my code (program) build with old compiler? 11 102
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. …
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
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 additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

679 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