Solved

Checking Handles

Posted on 1998-02-08
5
180 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: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

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
Quicksort a dynamic deque 33 82
C++ :Change value from  DisableCMD registry 4 79
Why isn't object file created? 6 106
How to Correctly derive class from CWinThread in MFC 4 113
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
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 viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
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.

732 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