🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: borland.public.cppbuilder.nativeapi
Article #16575

Re: Number of open files

#16575
From: hans galema
Date: Wed, 07 May 2003 15:53
97 lines
3696 bytes
Elias Berelian wrote:
> ...
> My first question is why I don't get the "too many files open" as is DOS/C?

Because you get a different message: fopen() will fail if too many files are 
open already.
Example:

void __fastcall TForm1::fopenButtonClick(TObject *Sender)
{
   Memo1->Clear();

   int filenr = 0;

   while ( ++ filenr <= maxwanted )
      {
       char path [MAXPATH+2];

       sprintf ( path, "fopen.%d.txt", filenr );

       if ( ! fopen ( path, "wb" ) )
         break;

       Memo1->Lines->Add ( path );
      }
}

> What is the best way to increase the allowable number? (Besides editing
> _nfiles.h and resetting _NFILE_.)

Use CreateFile(), WriteFile() and CloseHandle().
Then the amount is unlimited.
Example:

void __fastcall TForm1::CreateFileButtonClick(TObject *Sender)
{
   Memo1->Clear();

   int filenr = 0;

   while ( ++ filenr <= maxwanted ) /* private int maxwanted == 400 */
      {
       char path [MAXPATH+2];

       sprintf ( path, "CreateFile.%d.txt", filenr );

       // CreateFile
       HANDLE handle = CreateFile(
         path,                   // LPCTSTR lpFileName,	
         GENERIC_WRITE,          // DWORD dwDesiredAccess,	
         FILE_SHARE_WRITE,       // DWORD dwShareMode,	
         NULL,                   // LPSECURITY_ATTRIBUTES lpSecurityAttributes, 
         	CREATE_ALWAYS,          // DWORD dwCreationDistribution,	
         FILE_ATTRIBUTE_NORMAL,  // DWORD dwFlagsAndAttributes,	
         NULL                    // HANDLE hTemplateFile
         );

       if ( ! handle ) break;

       handles [filenr] = handle;
	/* private int array to keep the
	handles, for writing and closing later */

       Memo1->Lines->Add ( path );

       strcat ( path, "\r\n" );

       DWORD nwritten = 0;

       /* write path in the file */

       int done = WriteFile(
         handle,           //HANDLE hFile,	
         path,             //LPCVOID lpBuffer,	
         strlen ( path ),  //DWORD nNumberOfBytesToWrite,	
         &nwritten,        //LPDWORD lpNumberOfBytesWritten,	
         NULL              //LPOVERLAPPED lpOverlapped 	
         );

       if ( ! done )
         {
         Memo1->Lines->Add ( "Could not write to " + AnsiString ( path ) );

         break;
         }
      }
}
> Is there a way to figure out how many files are open?

Keep track of that yourself (need only one -global- counter or see above.

> What contributes to this number? (e.g. Does opening a socket count as an
> open file? How about a COM port?)

Don't know, but you can -now- easily try for yourself.

Hans.
Technical Details
Message-ID: <3eb92b9a$1@newsgroups.borland.com>
Path: arc1-ca.pugleaf.net!archiver1.google.com!news1.google.com!sn-xit-02!sn-xit-04!sn-xit-01!sn-xit-09!supernews.com!news.maxwell.syr.edu!newsfeed.wirehub.nl!news.kweb.on.ca!KWeb!newsgroups.borland.com!not-for-mail
References: <3eb91dac@newsgroups.borland.com>

Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit
X-Google-Thread: 10cc67,4a778e4dfe043211
X-Google-Attributes: gid10cc67,public
X-Google-ArrivalTime: 2003-05-07 08:58:40 PST
Date: Wed, 07 May 2003 17:53:25 +0200
From: hans galema <j.m.galema.dontusethis@maartens.nl>
User-Agent: Mozilla/5.0 (Windows; U; Win98; en-US; rv:1.2.1) Gecko/20021130
X-Accept-Language: en-us, en
MIME-Version: 1.0
Newsgroups: borland.public.cppbuilder.nativeapi
Subject: Re: Number of open files
References: <3eb91dac@newsgroups.borland.com>
In-Reply-To: <3eb91dac@newsgroups.borland.com>
Content-Type: text/plain; charset=us-ascii; format=flowed
Content-Transfer-Encoding: 7bit
NNTP-Posting-Host: 129.125.108.11
Message-ID: <3eb92b9a$1@newsgroups.borland.com>
X-Trace: newsgroups.borland.com 1052322714 129.125.108.11 (7 May 2003 08:51:54 -0700)
Lines: 98
Path: arc1-ca.pugleaf.net!archiver1.google.com!news1.google.com!sn-xit-02!sn-xit-04!sn-xit-01!sn-xit-09!supernews.com!news.maxwell.syr.edu!newsfeed.wirehub.nl!news.kweb.on.ca!KWeb!newsgroups.borland.com!not-for-mail