473,407 Members | 2,314 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,407 software developers and data experts.

File Limit of 1021

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;
}
Nov 21 '08 #1
9 1807
pa**********@gmail.com said:
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
Nov 21 '08 #2
pa**********@gmail.com writes:
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);
}

Nov 21 '08 #3
On 21 Nov, 08:10, paul.leme...@gmail.com wrote:
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)

Nov 21 '08 #4
pa**********@gmail.com wrote:
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?
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.
Nov 21 '08 #5
<pa**********@gmail.comwrote in message
news:1b**********************************@i24g2000 prf.googlegroups.com...
>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[i]);
Try taking out the //.

And what do you mean by 4K files?

--
Bartc

Nov 21 '08 #6
On 21 Nov, 10:28, Martin Ambuhl <mamb...@earthlink.netwrote:

<snip>
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

Nov 21 '08 #7
On Fri, 21 Nov 2008 00:10:40 -0800 (PST), pa**********@gmail.com
wrote:
>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[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
Nov 22 '08 #8
Barry Schwarz <sc******@dqel.comwrites:
On Fri, 21 Nov 2008 00:10:40 -0800 (PST), pa**********@gmail.com
wrote:
[...]
>>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) ks***@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"
Nov 22 '08 #9
pa**********@gmail.com wrote:
>
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.
Nov 22 '08 #10

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

Similar topics

11
by: Thomas Mlynarczyk | last post by:
Hello, I want to upload files via an HTML form and store them somewhere on my webspace. So far so good. I am just a bit concerned about security issues and traffic. My provider has set a file...
2
by: muser | last post by:
Karl this is albeit your program, in the program that you wrote for me I learned quite alot, i didn't realise I could return NULL for instance or even use functions without first declaring them,...
3
by: muser | last post by:
With the following code I'm trying to read a text file (infile) and output inaccuracies to the error file (printerfile). The text file is written and stored on disk, while the printerfile has to be...
1
by: muser | last post by:
The following program reads two lines of a file in my A drive and reads no more. Can some sarmartian tell me why that is. Please copy and paste the program into your own compiler please. ...
5
by: Jefferis NoSpamme | last post by:
Hi all, I'm trying to limit the file size of an image submission and I keep running into various problems. I've got most of it working, but I'm stumped and I have a basic question as to WHY this...
1
by: laredotornado | last post by:
Hi, I'm using PHP 4.4.4 on Apache 2 on Fedora Core 5. PHP was installed using Apache's apxs and the php library was installed to /usr/local/php. However, when I set my "error_reporting"...
9
by: eastcoastguyz | last post by:
I wrote a simple program to continue to create a very large file (on purpose), and even though there is plenty of disk space on that device the program aborted with the error message "File Size...
8
omerbutt
by: omerbutt | last post by:
hi there i have a form with multiple input (type/text ) fields and three inputs(type/file) fields i have to submit the form via ajax because i have multiple forms on this page ,you can say it is a...
1
KevinADC
by: KevinADC | last post by:
Note: You may skip to the end of the article if all you want is the perl code. Introduction Many websites have a form or a link you can use to download a file. You click a form button or click...
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.