Board index » cppbuilder » Amount of open files

Amount of open files


2005-08-03 05:48:54 PM
cppbuilder52
Hallo, is there any way to get the total amount of open files, opened by the
own application ?
Thanks for any help !
Andreas
 
 

Re:Amount of open files

Andreas Osswald wrote:
Quote
Hallo, is there any way to get the total amount of open files, opened
by the own application ?
What do you mean exactly?
If you want to know how many open files your application has at any one
time then I'd have to question your overall design and implementation.
IMO there should be no reason for you not to know that. A program that
doesn't know what state it's in sounds very dubious to me.
If you want to know how many files your application has opened in total
(ie;how many times has open() been called) then I can think of no
standard way to do that. The easiest way would be to have all file I/O
open requests go through your own routine that can keep a count. This
can only be enforced through programming convention but it's not a bad
idea.
We always use our own home grown functions (classes in more recent
code) because for us having that kind of centralised control over file
I/O offers many advantages. Logging all I/O is not something we bother
with at the moment but it would certainly be one application.
--
Andrue Cope [TeamB]
[Bicester, Uk]
info.borland.com/newsgroups/guide.html
 

Re:Amount of open files

I want to know how many files are opened by my application without closing
it. You are right if you say the programm should know this. But if you
forgot once to use close() or you returned out of a function before you call
close() a stream keeps open. Under DOS-OS there was the possibility to use
flushall() it returned all open streams an it was helpful to find errors
like described.
Andreas
"Andrue Cope [TeamB]" < XXXX@XXXXX.COM >schrieb im Newsbeitrag
Quote
Andreas Osswald wrote:

>Hallo, is there any way to get the total amount of open files, opened
>by the own application ?

What do you mean exactly?

If you want to know how many open files your application has at any one
time then I'd have to question your overall design and implementation.
IMO there should be no reason for you not to know that. A program that
doesn't know what state it's in sounds very dubious to me.

If you want to know how many files your application has opened in total
(ie;how many times has open() been called) then I can think of no
standard way to do that. The easiest way would be to have all file I/O
open requests go through your own routine that can keep a count. This
can only be enforced through programming convention but it's not a bad
idea.

We always use our own home grown functions (classes in more recent
code) because for us having that kind of centralised control over file
I/O offers many advantages. Logging all I/O is not something we bother
with at the moment but it would certainly be one application.

--
Andrue Cope [TeamB]
[Bicester, Uk]
info.borland.com/newsgroups/guide.html
 

{smallsort}

Re:Amount of open files

Andreas Osswald wrote:
Quote
But if you
forgot once to use close() or you returned out of a function before
you call close() a stream keeps open.
Hmmm. I'd call that careless programming :)
More seriously though that's why you shoul probably prefer class based
I/O (std::stream if you don't want to roll your own). They give you
built-in ownership symantics and you have to go out of our way to leave
one open.
But back to your question:This isn't really something the application
should have to do. It's a valid issue but I'd call it a bug. Bugs
should be resolved during debugging not left for the release code to
work around.
If your version of Builder has Code Guard (check for a [Code Guard] tab
on the project option) then use that. If not, download
www.automatedqa.com/downloads/memproof/
and while you're there check out the other stuff :)
--
Andrue Cope [TeamB]
[Bicester, Uk]
info.borland.com/newsgroups/guide.html
 

Re:Amount of open files

On Wed, 3 Aug 2005 14:31:02 +0200, "Andreas Osswald"
< XXXX@XXXXX.COM >wrote:
Quote
I want to know how many files are opened by my application without closing
it. You are right if you say the programm should know this. But if you
forgot once to use close() or you returned out of a function before you call
close() a stream keeps open. Under DOS-OS there was the possibility to use
flushall() it returned all open streams an it was helpful to find errors
like described.

Hi Andreas,
warning: hack ahead !
You could (temporarily) override the normal fopen / fclose functions
and do some book-keeping: I'd associate name and fd for each open file
and put it into a std::list. During fclose, you can then search for
the entry holding this particular fd and remove it. At program
termination this list will of course be empty <g>.
HTH
Helmut Giese
// new file with replacements for fopen & fclose
#include <stdio>
using std::FILE;
using std::printf;
FILE *my_fopen( const char *name, const char *mode)
{
FILE *fd = std::fopen( name, mode);
// show /who/ opens the file
printf( "my_fopen opening %s and returning %p\n", name, fd);
// save name and fd in a globally accessable structure
return fd;
}
int my_fclose( FILE *fd)
{
// show /who/ closes the file
printf( "my_fclose closing %p\n", fd);
// search for fd in the data saved during open and remove the entry
return std::fclose( fd);
}
// end of file
// test file
// all other files should see (via a header file) the following 2
#defines
#define fopen my_fopen
#define fclose my_fclose
#include <stdio>
using std::FILE;
int main( void)
{
FILE *fd = fopen( "test.txt", "w");
fprintf( fd, "Hi there.\n");
fclose( fd);
return 0;
}
 

Re:Amount of open files

Andreas Osswald < XXXX@XXXXX.COM >wrote:
Quote
I want to know how many files are opened by my application without
closing
it. You are right if you say the programm should know this. But if you
forgot once to use close() or you returned out of a function before you
call
close() a stream keeps open. Under DOS-OS there was the possibility to
use
flushall() it returned all open streams an it was helpful to find errors
like described.

Andreas

For debug purposes this might be an help.
www.sysinternals.com/Utilities/Filemon.html
--
JF Jolin
 

Re:Amount of open files

Andreas Osswald wrote:
Quote
I want to know how many files are opened by my application without closing
it. You are right if you say the programm should know this. But if you
forgot once to use close() or you returned out of a function before you call
close() a stream keeps open. Under DOS-OS there was the possibility to use
flushall() it returned all open streams an it was helpful to find errors
like described.

Andreas


"Andrue Cope [TeamB]" < XXXX@XXXXX.COM >schrieb im Newsbeitrag
news:42f095b9$ XXXX@XXXXX.COM ...

>Andreas Osswald wrote:
>
>
>>Hallo, is there any way to get the total amount of open files, opened
>>by the own application ?
>
>What do you mean exactly?
>
>If you want to know how many open files your application has at any one
>time then I'd have to question your overall design and implementation.
>IMO there should be no reason for you not to know that. A program that
>doesn't know what state it's in sounds very dubious to me.
>
>If you want to know how many files your application has opened in total
>(ie;how many times has open() been called) then I can think of no
>standard way to do that. The easiest way would be to have all file I/O
>open requests go through your own routine that can keep a count. This
>can only be enforced through programming convention but it's not a bad
>idea.
>
>We always use our own home grown functions (classes in more recent
>code) because for us having that kind of centralised control over file
>I/O offers many advantages. Logging all I/O is not something we bother
>with at the moment but it would certainly be one application.
>
>--
>Andrue Cope [TeamB]
>[Bicester, Uk]
>info.borland.com/newsgroups/guide.html



Andreas,
Did you compile your code with CodeGuard? CodeGuard must detect and
report this kind of errors.
Sabetay
 

Re:Amount of open files

Sabetay Toros wrote:
Quote
Andreas,

Did you compile your code with CodeGuard? CodeGuard must detect and
report this kind of errors.

Sabetay
Could you trim your quoting please? You've added 5 lines of your own
and reposted several dozen of mine. Newsgroup guidlines (below my sig)
ask that you post only what's needed to maintain context. Thanks.
--
Andrue Cope [TeamB]
[Bicester, Uk]
info.borland.com/newsgroups/guide.html