Connecting Tech Pros Worldwide Help | Site Map

File Limit of 1021

  #1  
Old November 21st, 2008, 08:15 AM
paul.lemelle@gmail.com
Guest
 
Posts: n/a
I am trying to create a series of 4K files, everything works fine
until I pass the 1022 mark - I get an error stating that he file
cannot be opened.

Can someone look at the below code and offer a suggestion for a
solution?

__

#include <stdio.h>
#include <stdlib.h>

int main()
{
FILE **fp;
char filename[50];
long i;
long count;
long number;
int k = 32000;
long datasize;

datasize = 4 * k;

printf("How many files to create: ");
scanf("%d\n", &number);


fp = malloc( sizeof(FILE *) * number);

if( fp != NULL )
{
for(i=0; i < number; i++)
{
sprintf(filename, "%s%d.txt", "file",i+1);

if( ( fp[i] = fopen(filename, "a+") ) == NULL )
{
printf("Error: File \"%s\" cannot be opened\n", filename);
continue;
}
count = 0;
while (count < datasize)
{


fprintf(fp[i],"%s","1234567812345678123456781234678\n");
count ++;

}

//fclose(fp[i]);




}
}
else
{
printf("Error: Not enough memory\n");
getchar();
return 1;
}

free(fp);

printf("All done\n");
getchar();
return 0;
}
  #2  
Old November 21st, 2008, 08:25 AM
Richard Heathfield
Guest
 
Posts: n/a

re: File Limit of 1021


paul.lemelle@gmail.com said:
Quote:
I am trying to create a series of 4K files, everything works fine
until I pass the 1022 mark - I get an error stating that he file
cannot be opened.
Try closing a few.

Implementations don't have to offer you more than FOPEN_MAX files open at a
time, and FOPEN_MAX is allowed to be as low as 8 (INCLUDING stdin, stdout,
and stderr). It looks like yours is 1024. Taking off those three I just
mentioned, that would make 1021, which does seem to explain your problem,
doesn't it?

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
  #3  
Old November 21st, 2008, 08:25 AM
Nate Eldredge
Guest
 
Posts: n/a

re: File Limit of 1021


paul.lemelle@gmail.com writes:
Quote:
I am trying to create a series of 4K files, everything works fine
until I pass the 1022 mark - I get an error stating that he file
cannot be opened.
>
Can someone look at the below code and offer a suggestion for a
solution?
>
__
>
#include <stdio.h>
#include <stdlib.h>
>
int main()
{
FILE **fp;
char filename[50];
long i;
long count;
long number;
int k = 32000;
long datasize;
>
datasize = 4 * k;
>
printf("How many files to create: ");
scanf("%d\n", &number);
>
>
fp = malloc( sizeof(FILE *) * number);
>
if( fp != NULL )
{
for(i=0; i < number; i++)
{
sprintf(filename, "%s%d.txt", "file",i+1);
>
if( ( fp[i] = fopen(filename, "a+") ) == NULL )
{
printf("Error: File \"%s\" cannot be opened\n", filename);
continue;
}
count = 0;
while (count < datasize)
{
>
>
fprintf(fp[i],"%s","1234567812345678123456781234678\n");
count ++;
>
}
>
//fclose(fp[i]);
>
>
>
>
}
}
else
{
printf("Error: Not enough memory\n");
getchar();
return 1;
}
>
free(fp);
>
printf("All done\n");
getchar();
return 0;
}
Most OS'es have a limit on the number of files that can be open at one
time. You're probably running into yours; 1024 is a common value for
the limit. You might be able to increase it, but that's between you and
your OS and not really relevant to comp.lang.c. Your OS's documentation
will probably say something about it.

But it looks like you already have the solution: you only need to write
one file at a time, so just close it before you open the next one.
Uncommenting the fclose line in your code would do that. The array is
not really necessary either; you could just reuse the same FILE *.

int i;
FILE *f;
char filename[50];
/* ... */
for (i = 0; i < number; i++) {
sprintf(filename, ...);
f = fopen(filename, "w");
fprintf(f, ...);
fclose(f);
}

  #4  
Old November 21st, 2008, 10:25 AM
Nick Keighley
Guest
 
Posts: n/a

re: File Limit of 1021


On 21 Nov, 08:10, paul.leme...@gmail.com wrote:
Quote:
I am trying to create a series of 4K files, everything works fine
until I pass the 1022 mark - I get an error stating that he file
cannot be opened.
>
Can someone look at the below code and offer a suggestion for a
solution?
your program layout is horrid. This is the layout cleaned up
with a few comments

/
************************************************** **************************/

#include <stdio.h>
#include <stdlib.h>

int main ()
/*** njk better style is
int main (void)
***/

{
FILE **fp;
/*** njk this is unusual ***/

char filename [50];
/*** njk magic number ***/

long i;
long count;
long number;
int k = 32000;
/*** njk magic number and/or unhelpful variable name ***/
/*** njk was k supposed to be 1024? ***/

long datasize;

datasize = 4 * k;

printf ("How many files to create: ");
/***njk you need an fflush(stdout) ***/

scanf ("%d\n", &number);
/*** njk no error check on scanf() ***/

fp = malloc (sizeof(FILE*) * number);

if (fp != NULL)
{
for (i = 0; i < number; i++)
{
sprintf (filename, "%s%d.txt", "file", i + 1);
/*** njk why not
sprintf ("file%d.txt", i + 1);
***/

/*** njk you only use one file at a time. Why do you have an array of
FILE*s? ***/

if ((fp [i] = fopen (filename, "a+")) == NULL)
{
printf("Error: File \"%s\" cannot be opened\n",
filename);
continue;
}

count = 0;
while (count < datasize)
{
fprintf (fp[i],"%s",
"1234567812345678123456781234678\n");
/*** njk why not
fprintf (fp[i], "1234567812345678123456781234678\n");
***/

count++;
}

//fclose(fp[i]);
/*** njk why have you commented this out? ***/
}
}
else
{
printf("Error: Not enough memory\n");
getchar();
/*** njk non-standard function ***/

return 1;
/*** njk non-standard return from main() ***/
}

free(fp);

printf("All done\n");
getchar();

return 0;
}



--
Nick Keighley
printf() is your debugging friend! (We've all needed to
set breakpoints or pore over coredumps back in the day,
but judicious use of "if (TEST) printf()" is the
straightforward way to address most testing and debugging.)
(James Dow Allen clc)

  #5  
Old November 21st, 2008, 10:35 AM
Martin Ambuhl
Guest
 
Posts: n/a

re: File Limit of 1021


paul.lemelle@gmail.com wrote:
Quote:
I am trying to create a series of 4K files, everything works fine
until I pass the 1022 mark - I get an error stating that he file
cannot be opened.
Quote:
Can someone look at the below code and offer a suggestion for a
solution?
It is amazing that someone who is so profligate in wasting system
resources is so stingy with whitespace. Learn to indent code so it is
readable. Your problem is that your are wasteful in several ways:
1) You are pointlessly creating an array of FILE * objects.
2) You are pointlessly not closing files when you are done with them.
The surprise is that your system is so generous that it lets you have
1022 files open at once.
And your claimed specification is not true. Your files are not 4K but
4125K.
  #6  
Old November 21st, 2008, 11:05 AM
Bartc
Guest
 
Posts: n/a

re: File Limit of 1021


<paul.lemelle@gmail.comwrote in message
news:1bd01fb1-0122-497f-8abc-68a26fc3d48e@i24g2000prf.googlegroups.com...
Quote:
>I am trying to create a series of 4K files, everything works fine
until I pass the 1022 mark - I get an error stating that he file
cannot be opened.
>
Can someone look at the below code and offer a suggestion for a
solution?
Quote:
//fclose(fp[i]);
Try taking out the //.

And what do you mean by 4K files?

--
Bartc

  #7  
Old November 21st, 2008, 11:45 AM
Nick Keighley
Guest
 
Posts: n/a

re: File Limit of 1021


On 21 Nov, 10:28, Martin Ambuhl <mamb...@earthlink.netwrote:

<snip>
Quote:
And your claimed specification is not true. Your files are not 4K but
4125K.
you (the original poster) do datasize writes to each file.
Each write consists of 32 digits plus a newline. That's 33 bytes
per write. datasize is 4 * k and k is 32000. So that's

33 * 4 * 32000 = 4224000 bytes = 4125 k (where k=1024)

--
Nick Keighley

  #8  
Old November 22nd, 2008, 01:25 AM
Barry Schwarz
Guest
 
Posts: n/a

re: File Limit of 1021


On Fri, 21 Nov 2008 00:10:40 -0800 (PST), paul.lemelle@gmail.com
wrote:
Quote:
>I am trying to create a series of 4K files, everything works fine
>until I pass the 1022 mark - I get an error stating that he file
>cannot be opened.
>
>Can someone look at the below code and offer a suggestion for a
>solution?
>
>__
>
>#include <stdio.h>
>#include <stdlib.h>
>
>int main()
>{
>FILE **fp;
>char filename[50];
>long i;
>long count;
>long number;
>int k = 32000;
>long datasize;
>
>datasize = 4 * k;
If int is 16 bits on your system, this computation will fail. Try 4L
instead.
Quote:
>
>printf("How many files to create: ");
>scanf("%d\n", &number);
>
>
>fp = malloc( sizeof(FILE *) * number);
>
>if( fp != NULL )
>{
>for(i=0; i < number; i++)
>{
>sprintf(filename, "%s%d.txt", "file",i+1);
>
>if( ( fp[i] = fopen(filename, "a+") ) == NULL )
>{
>printf("Error: File \"%s\" cannot be opened\n", filename);
>continue;
>}
>count = 0;
>while (count < datasize)
>{
>
>
>fprintf(fp[i],"%s","1234567812345678123456781234678\n");
>count ++;
>
>}
>
>//fclose(fp[i]);
>
>
>
>
>}
>}
>else
>{
>printf("Error: Not enough memory\n");
>getchar();
>return 1;
>}
>
>free(fp);
>
>printf("All done\n");
>getchar();
>return 0;
>}
--
Remove del for email
  #9  
Old November 22nd, 2008, 01:45 AM
Keith Thompson
Guest
 
Posts: n/a

re: File Limit of 1021


Barry Schwarz <schwarzb@dqel.comwrites:
Quote:
On Fri, 21 Nov 2008 00:10:40 -0800 (PST), paul.lemelle@gmail.com
wrote:
[...]
Quote:
Quote:
>>int k = 32000;
>>long datasize;
>>
>>datasize = 4 * k;
>
If int is 16 bits on your system, this computation will fail. Try 4L
instead.
[...]

Better, declare k as long. And since it's not used anywhere other
than in the computation of datasize, and thus is never modified, make
it const.

Indenting the code would also be helpful.

--
Keith Thompson (The_Other_Keith) kst-u@mib.org <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
  #10  
Old November 22nd, 2008, 02:35 AM
CBFalconer
Guest
 
Posts: n/a

re: File Limit of 1021


paul.lemelle@gmail.com wrote:
Quote:
>
I am trying to create a series of 4K files, everything works fine
until I pass the 1022 mark - I get an error stating that he file
cannot be opened.
You mean "a count of 1022 files". You have run into an OS
limitation. Depending on system, there may be a way around it, but
it is OT here. Try a news group that deals with your system.

--
[mail]: Chuck F (cbfalconer at maineline dot net)
[page]: <http://cbfalconer.home.att.net>
Try the download section.
Closed Thread


Similar Threads
Thread Thread Starter Forum Replies Last Post
(part 37) Han from China answers your C questions Borked Pseudo Mailed answers 0 November 21st, 2008 02:45 PM
Out of Memory error sphenxes@gmail.com answers 3 June 27th, 2008 05:46 PM
How many connections can accept a 'binded' socket? billiejoex answers 4 March 21st, 2007 10:25 AM
thread limit in python danieldsmith@gmail.com answers 11 August 14th, 2005 12:05 AM