fopen failed when called several time.

P

Patrice

Hi,
I would to call fopen function several time in my application.
This application permits to read files which path is registered in a
configuration file.
For exemple:
File 1 = toto.txt
File 2 = tot2.txt
....File N = TotoN.txt
Then, I read each file (one by one), get adress of the beginning of
data and size of data.
I use, this function:
static bool OpenFile(char* aSrcPath, uint8** aBuffer, uint32*
aBufferSize)
{
FILE* theInputFile = 0;
uint8* theBuffer = 0;
uint32 theBufferSize = 0;
uint32 theSizeRead = 0;

/********************/
/* we open the file */
/********************/

if ( (theInputFile = fopen(aSrcPath, "rb")) == 0 )
{
return false;
}

/* ********************************* */
/* *** we must to know file size *** */
/* ********************************* */

if ( fseek( theInputFile, 0, SEEK_END ) != 0)
{
fclose(theInputFile);
return false;
}

if ( (theBufferSize = ftell(theInputFile)) == -1L )
{
fclose(theInputFile);
return false;
}

/* ************************** */
/* *** we allocate buffer *** */
/* ************************** */

if ((theBuffer = (uint8*)malloc(theBufferSize))==0)
{
fclose(theInputFile);
return false;
}

/* ****************************** */
/* *** we read file from disk *** */
/* ****************************** */

if ( fseek( theInputFile, 0, SEEK_SET ) != 0)
{
fclose(theInputFile);
freeBuffer(&theBuffer, &theBufferSize);
return false;
}

theSizeRead = fread( theBuffer, sizeof(uint8), theBufferSize,
theInputFile );

if( theSizeRead != theBufferSize )
{
fclose(theInputFile);
freeBuffer(&theBuffer, &theBufferSize);
return false;
}

/* ************************* */
/* *** we can close file *** */
/* ************************* */

fclose(theInputFile);

/* *********** */
/* All it's ok */
/* *********** */
*aBuffer = theBuffer;
*aBufferSize = theBufferSize;
return true;
}

The problem is when I reach 260 calls (to open my 260th file in config
file) of this function fopen failed.
And each time I use it from this point all fopen failed.
I already check path, and when I close my application and I start to
read from the 260th file, it works until 520th file ...

Thanks for your advices,

Patrice.
 
J

jacob navia

Patrice said:
Hi,
I would to call fopen function several time in my application.
This application permits to read files which path is registered in a
configuration file.
For exemple:
File 1 = toto.txt
File 2 = tot2.txt
...File N = TotoN.txt
Then, I read each file (one by one), get adress of the beginning of
data and size of data.
I use, this function:
static bool OpenFile(char* aSrcPath, uint8** aBuffer, uint32*
aBufferSize)
{
FILE* theInputFile = 0;
uint8* theBuffer = 0;
uint32 theBufferSize = 0;
uint32 theSizeRead = 0;

/********************/
/* we open the file */
/********************/

if ( (theInputFile = fopen(aSrcPath, "rb")) == 0 )
{
return false;
}

/* ********************************* */
/* *** we must to know file size *** */
/* ********************************* */

if ( fseek( theInputFile, 0, SEEK_END ) != 0)
{
fclose(theInputFile);
return false;
}

if ( (theBufferSize = ftell(theInputFile)) == -1L )
{
fclose(theInputFile);
return false;
}

/* ************************** */
/* *** we allocate buffer *** */
/* ************************** */

if ((theBuffer = (uint8*)malloc(theBufferSize))==0)
{
fclose(theInputFile);
return false;
}

/* ****************************** */
/* *** we read file from disk *** */
/* ****************************** */

if ( fseek( theInputFile, 0, SEEK_SET ) != 0)
{
fclose(theInputFile);
freeBuffer(&theBuffer, &theBufferSize);
return false;
}

theSizeRead = fread( theBuffer, sizeof(uint8), theBufferSize,
theInputFile );

if( theSizeRead != theBufferSize )
{
fclose(theInputFile);
freeBuffer(&theBuffer, &theBufferSize);
return false;
}

/* ************************* */
/* *** we can close file *** */
/* ************************* */

fclose(theInputFile);

/* *********** */
/* All it's ok */
/* *********** */
*aBuffer = theBuffer;
*aBufferSize = theBufferSize;
return true;
}

The problem is when I reach 260 calls (to open my 260th file in config
file) of this function fopen failed.
And each time I use it from this point all fopen failed.
I already check path, and when I close my application and I start to
read from the 260th file, it works until 520th file ...

Thanks for your advices,

Patrice.

It doesn't look like the problem is in the function you show. I do not
see any obvious errors, so the problem could be elsewhere.

fopen has a limit of open files, and if the calling function is opening
files without closing them, the program fails.
Look at all fopen calls in your program and see if you have forgotten
an fclose somewhere.
 
?

=?ISO-8859-1?Q?=22Nils_O=2E_Sel=E5sdal=22?=

Patrice said:
The problem is when I reach 260 calls (to open my 260th file in config
file) of this function fopen failed.
And each time I use it from this point all fopen failed.
I already check path, and when I close my application and I start to
read from the 260th file, it works until 520th file ...
Looks like your system has enforced a limit on how many
files you can have open at a given time. You should fclose
a file when you're done with it.
 
J

jacob navia

Nils said:
Looks like your system has enforced a limit on how many
files you can have open at a given time. You should fclose
a file when you're done with it.
The file is closed in the code the OP posted.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,073
Latest member
DarinCeden

Latest Threads

Top