File Limit of 1021

P

paul.lemelle

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 = fopen(filename, "a+") ) == NULL )
{
printf("Error: File \"%s\" cannot be opened\n", filename);
continue;
}
count = 0;
while (count < datasize)
{


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

}

//fclose(fp);




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

free(fp);

printf("All done\n");
getchar();
return 0;
}
 
N

Nate Eldredge

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 = fopen(filename, "a+") ) == NULL )
{
printf("Error: File \"%s\" cannot be opened\n", filename);
continue;
}
count = 0;
while (count < datasize)
{


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

}

//fclose(fp);




}
}
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);
}
 
N

Nick Keighley

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 = fopen (filename, "a+")) == NULL)
{
printf("Error: File \"%s\" cannot be opened\n",
filename);
continue;
}

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

count++;
}

//fclose(fp);
/*** 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)
 
B

Bartc

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?
//fclose(fp);


Try taking out the //.

And what do you mean by 4K files?
 
N

Nick Keighley

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)
 
B

Barry Schwarz

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.
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 = fopen(filename, "a+") ) == NULL )
{
printf("Error: File \"%s\" cannot be opened\n", filename);
continue;
}
count = 0;
while (count < datasize)
{


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

}

//fclose(fp);




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

free(fp);

printf("All done\n");
getchar();
return 0;
}
 
K

Keith Thompson

Barry Schwarz said:
[...]
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.
 
C

CBFalconer

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.
 

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,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top