il risultato puo esplodere e diciamo che 1000 sono pochini...
ma sei sicuro che ti servano così tanti file ? considera che generalmente
gli utenti odiano aver a che fare con troppi file.
puoi usare funzioni tipo itoa, es.
codice:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>
static int mkNfiles(const char *bname, int nf);
static void mkfile(const char *bname, const char *fid);
static char *itoa(int val);
static int hidgt_eval(int n);
static int dgt_cnt(int n);
int main(void)
{
int n;
char *bname = "nome_file_";
n = mkNfiles(bname, 89761);
printf("%d files have been successfully created.\n", n);
return 0;
}
/**
* mkNfiles - creates "nf" files and returns the number of
* successfully created files.
*
* @bname: base name for files.
* @nf : number of files to create.
*
**/
int mkNfiles(const char *bname, int nf)
{
register int i = 0;
char *fid;
while (i < nf) {
fid = itoa(i);
mkfile(bname, fid);
free(fid);
++i;
}
return i;
}
/**
* mkfile - creates a file.
*
* @bname: base name for file.
* @fid : suffix to append to file name.
*
**/
void mkfile(const char *bname, const char *fid)
{
size_t len;
char *fPath;
FILE *fnew;
len = strlen(bname) + strlen(fid) + 1;
fPath = calloc(len, sizeof (char));
if (fPath == NULL) {
printf("Error: out of memory: calloc() failed.\n");
(void) getchar();
exit(EXIT_FAILURE);
}
strcpy(fPath, bname);
strcat(fPath, fid);
fnew = fopen(fPath, "w");
if (fnew == NULL) {
printf("Error: unable to create file %s\n", fPath);
(void) getchar();
exit(EXIT_FAILURE);
}
if (fclose(fnew) != 0) {
printf("Warning: unable to close file %s\n", fPath);
(void) getchar();
exit(EXIT_FAILURE);
}
free(fPath);
return;
}
/**
* itoa - This function returns a pointer to the string equivalent
* of "val". Allocated memory must be freed by the user.
*
* @val : a number.
*
**/
char *itoa(int val)
{
register int i;
register int hc;
register int len;
int ndgt[2];
char *str;
len = dgt_cnt(val);
str = calloc(len + 1, sizeof (char));
if (str == NULL) {
printf("Error: out of memory: calloc() failed.\n");
(void) getchar();
exit(EXIT_FAILURE);
}
for (i = 0; i < len; ++i)
*(str + i) = '0';
*(str + len + 1) = '\0';
for (i = 1 ; i < len ; ++i) {
hc = hidgt_eval(val);
*(str + (i - 1)) = (char) (hc + 48);
ndgt[0] = dgt_cnt(val);
val = val - (hc * pow(10, (len - i)));
ndgt[1] = dgt_cnt(val);
while ((ndgt[1] + 1) < ndgt[0]--) {
++i;
*(str + (i - 1)) = '0';
}
}
if (val < 10)
*(str + (i - 1)) = (char) (val + 48);
return str;
}
/**
* hidgt_eval - evaluates the most significant digit
* of a given int.
*
* @n: a number.
*
**/
int hidgt_eval(int n)
{
for (; (n /= 10) >= 10; )
; /* Void */
return n;
}
/**
* dgt_cnt - count the number of digits of "n" and returns it.
*
* @n: a number.
*
**/
int dgt_cnt(int n)
{
register int cnt = 1;
for (; n >= 10; cnt++)
n /= 10;
return cnt;
}
è solo un esempio, non è ottimizzato per niente...semplicemente funziona
ah, cambia il numero di file se intendi provarlo...89761 sono veramente tanti