ciau
se ho una stringa contenente "prog=0.64155436" come faccio a trasformarla in una con dentro 64,15% ??
ciau
se ho una stringa contenente "prog=0.64155436" come faccio a trasformarla in una con dentro 64,15% ??
Originariamente inviato da TestaDiMucca
ciau ;)
se ho una stringa contenente "prog=0.64155436" come faccio a trasformarla in una con dentro 64,15% ??
:DDovrebbe andare :)codice:/* proviamo.c - by michele ;) */ #include <stdlib.h> #include <stdio.h> #include <string.h> char *f (char *); int main (void) { char str[] = "prog=0.64155436"; printf ("%s \n", f (str)); return (0); } char *f (char *s) { float f; char *p = (char *) malloc (sizeof (s)); if (strstr (s, "prog=")) s = s + 5; f = atof (s); f = f * 100; sprintf (p, "%2.2f%%", f); return (p); }
michele
michele.facchin@virgilio.it
Server:irc.azzurra.org | /join #c
ma com'è che se implemento il tuo codice nel mio prog, non funzia ?
come output mi da :codice:#include <stdio.h> #include <stdio.h> #include <string.h> char *f (char *); int main(void) { char done[100]; char *stato; FILE *file; file = fopen("state.sah", "r"); /* open a text file for reading */ if(file==NULL) { printf("Seti@Home ha finito di elaborare la WorkUnit.\n"); return 1; } else { while(fgets(done, 100, file)!=NULL) { if(strstr(done,"prog=0.")) { printf("%s",done); printf ("%s \n", f (done)); } } fclose(file); system ("PAUSE"); return 0; } } char *f (char *s) { float f; char *p = (char *) malloc (sizeof (s)); if (strstr (s, "prog=")) s = s + 5; f = atof (s); f = f * 100; sprintf (p, "%2.2f%%", f); return (p); }
prog=0.64155436
229339200.00%
Hai messo due volte stdio, sostituisci con stdlib.
michele
michele.facchin@virgilio.it
Server:irc.azzurra.org | /join #c