Article View: borland.public.cppbuilder.nativeapi
Article #5589Re: Amount of open files
From: Helmut Giese
Date: Wed, 03 Aug 2005 14:53
Date: Wed, 03 Aug 2005 14:53
65 lines
2704 bytes
2704 bytes
On Wed, 3 Aug 2005 14:31:02 +0200, "Andreas Osswald"
<noname@noname.de> wrote:
>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;
}
Technical Details
Message-ID:
<42f0d848.12370625@forums.borland.com>Path:
arc1-ca.pugleaf.net!g2news1.google.com!news2.google.com!newsfeed.gamma.ru!Gamma.RU!newsfeed.icl.net!newsfeed.fjserv.net!oleane.net!oleane!grolier!194.74.65.66.MISMATCH!news-peer0-test!news-peer-test!btnet!newsfeed.bt.es!caladan.landsraad.net!newsgroups.borland.com!not-for-mailReferences:
<42f09312$1@newsgroups.borland.com> <42f095b9$1@newsgroups.borland.com> <42f0b912$1@newsgroups.borland.com>Show Full Headers
Complete Article Headers
X-Google-Thread: 10cc67,6a0cee41cdcb7499 X-Google-Attributes: gid10cc67,public X-Google-Language: ENGLISH,ASCII-7-bit From: hgiese@ratiosoft.com (Helmut Giese) Newsgroups: borland.public.cppbuilder.nativeapi Subject: Re: Amount of open files Date: Wed, 03 Aug 2005 14:53:53 GMT Message-ID: <42f0d848.12370625@forums.borland.com> References: <42f09312$1@newsgroups.borland.com> <42f095b9$1@newsgroups.borland.com> <42f0b912$1@newsgroups.borland.com> X-Newsreader: Forte Free Agent 1.21/32.243 NNTP-Posting-Host: 141.51.159.163 X-Trace: newsgroups.borland.com 1123080520 141.51.159.163 (3 Aug 2005 07:48:40 -0700) Lines: 66 Path: arc1-ca.pugleaf.net!g2news1.google.com!news2.google.com!newsfeed.gamma.ru!Gamma.RU!newsfeed.icl.net!newsfeed.fjserv.net!oleane.net!oleane!grolier!194.74.65.66.MISMATCH!news-peer0-test!news-peer-test!btnet!newsfeed.bt.es!caladan.landsraad.net!newsgroups.borland.com!not-for-mail