PDA

Visualizza la versione completa : [C/C++]: Creazione di file e loro numerazione


billybilly
08-08-2006, 20:07
ciao,
sto facendo un programmino in c che mi crea un numero direi "stratosferico" di file.
per nominarli in modo differente ho pensato di ricorrere a un contatore....ma anche facendo un long double è troppo piccolo per le mie necessità.
ho cosi creato un char* x che inizialmente vale "1\0"...in pratica ho fatto una stringa.
Ogni volta che creo un file incremento il valore del mio char * tramite un apposita funzione...ma... nel momento in cui faccio un assegnamento del tipo : x[0]="1" (ho provato anche con apici singoli..... mi da un errore di segmention fault.....perchè????
se uqalcuno riesce riesci a darmi una risposta ne sarei molto grato.
Saluti Diego

U-bahn
08-08-2006, 21:13
es.


#include <stdio.h>
#include <string.h>

int main(void)
{
char fname[14] = "0";
char id[4] = "000";
FILE *fnew;

do {
strcpy(fname, "nome_file_");
strcat(fname, id);
fnew = fopen(fname, "w");
fclose(fnew);

++id[2];
if (id[2] == ':') {
++id[1];
if (id[1] == ':') {
++id[0];
id[1] = '0';
}
id[2] = '0';
}
} while (id[0] != ':') ;
return 0;
}
questo crea 1000 file...ovviamente usando dei puntatori il tutto risulterebbe
più "malleabile".

:ciauz:

billybilly
08-08-2006, 21:20
ciao :)
grazie per il consiglio...ma in questo modo ero riuscito...
il problema e che non so quanti me ne crei.... il risultato puo esplodere e diciamo che 1000 sono pochini....o anche rendendolo piu grande incrementando anche di parecchio l'ordine di grandezza...potrebbe non bastarmi -_-' ho bisogno di fare una cosa dinamica..... tipo un array non dimensionato con un carattere terminatore e a ogni ciclo di creazione del file.... controllare l'ultimo carattere incrementarlo di uno (valutando i casi 99999) e nel caso del 9 in ultima posizione creare un nuova cella dell'array e aumentare il numero.....questa era l'idea.... solo che mi da quell'errore se provo a farla -_-'.... hai qualche altra idea???
grazie per ora :)

billybilly
08-08-2006, 21:34
seplicemente io faccio una cosa del genere....
char *x:
x="1\0" //creao la stringa contenente solo 1
.....
poi se provo a fare :
x[0]='2'
0ppure
x[0]="2"
mi da errore di segmention fault....
ma io voglio assegnargli 2 in quella pos....come fare??

U-bahn
09-08-2006, 05:35
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. :bhò:


....come fare??
puoi usare funzioni tipo itoa, es.


#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 :D

:ciauz:

U-bahn
09-08-2006, 09:14
visto che non ho niente di meglio da fare :D

una soluzione "leggermente" più pulita:


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

int dgt_cnt(int n);

int main(void)
{
int i;
int l;
char *buf;
char *base = "nome_file_"; /* 10 + 1 */
char *name;
FILE *fnew;

/* Devi ottenere in qualche modo il valore di NN */
for (i = 0; i < NN; ++i) {
buf = calloc(dgt_cnt(i) + 1, sizeof (char));
sprintf(buf, "%d", i);
l = strlen(buf) + 11;
name = calloc(l, sizeof (char));
strcpy(name, base);
strcat(name, buf);
fnew = fopen(name, "w");
free(buf);
free(name);
fclose(fnew);

}
return 0;
}

int dgt_cnt(int n)
{
register int cnt = 1;

for (; n >= 10; cnt++)
n /= 10;
return cnt;
}

Loading