473,395 Members | 2,446 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

fopen failed when called several time.

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.
Nov 14 '05 #1
3 2559
Patrice wrote:
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.
Nov 14 '05 #2
Patrice wrote:

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.

--
Nils O. Selåsdal
www.utelsystems.com
Nov 14 '05 #3
Nils O. Selåsdal wrote:
Patrice wrote:

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.

The file is closed in the code the OP posted.
Nov 14 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Loopy | last post by:
I'm trying to write a script that will connect to an external site so that I can get access referer information in a database on another one of my sites. I got errors, so I tried to write a script...
5
by: Arturo Ordaz | last post by:
Has anyone run into documentation that describes how fopen(log_file, "a") behaves when it is potentialy used (called/referenced) 1000's of times an hour on a single resource file? I am trying to...
28
by: Sathyaish | last post by:
If fopen fails, is there a way to know why?
10
by: pjlsr | last post by:
It's close to twenty years since I used the C language and at that time I was doing only floating point computational work, nothing with strings or reading files. I tried to use fopen in the...
9
by: John Kirksey | last post by:
I have a page that uses an in-place editable DataGrid that supports sorting and paging. EnableViewState is turned ON. At the top of the page are several search fields that allow the user to filter...
4
by: lucyachammond | last post by:
I have a 3rd party real-estate website written in php. I would like to call an asp script on a separate webserver each time a new real-estate item is added to the database on the php site. The...
25
by: subramanian100in | last post by:
Consider the following program: #include <stdio.h> #include <stdlib.h> int main(int argc, char *argv) { if (argc != 2) { printf("Usage: <program-name<text-file>\n");
0
by: milan.topalov | last post by:
I'm having some strange problems using fopen() to fetch website content. It seems I can't open URL shorter then certain lenght. I can open this:...
20
by: cscorley | last post by:
For some reason, I cannot use fopen() on the file in write mode. The file "time" is in the same directory as the .php file, with permissions set to 0766. PHP Version 5.2.5 Apache/2.2.8 code...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.