🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Thread View: borland.public.cppbuilder.nativeapi
2 messages
2 total messages Started by Elias Berelian Wed, 07 May 2003 14:53
Number of open files
#16573
Author: Elias Berelian
Date: Wed, 07 May 2003 14:53
13 lines
1531 bytes
I use BC++B 5. But run the exe in NT, 98, 2000, and XP.
The problem I have is that I have to open more than 50 (but less than 100)
files. Most files I open at startup. But a few files I open while running. I
also open server sockets (about 100) on different ports.
My first question is why I don't get the "too many files open" as is DOS/C?
What is the best way to increase the allowable number? (Besides editing
_nfiles.h and resetting _NFILE_.)
Is there a way to figure out how many files are open?
What contributes to this number? (e.g. Does opening a socket count as an
open file? How about a COM port?)
Am I in the right newsgroup?

Regards, Elias
Re: Number of open files
#16575
Author: 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.
Thread Navigation

This is a paginated view of messages in the thread with full content displayed inline.

Messages are displayed in chronological order, with the original post highlighted in green.

Use pagination controls to navigate through all messages in large threads.

Back to All Threads