Solved

Checking Handles

Posted on 1998-02-08
5
178 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: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

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
c++ how to tell if the progra is ctl or mfc atl ect 6 84
Find Visual Studio Tools 2 111
Create a path if not exists 7 89
Installshield for Embarcadero EX 10.1 Berlin 4 60
Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
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 user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

828 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