Hi to all, I have that string "prog=0.64155436" and I want to manipulate it until it will be like "64.15%".
I've written a small code that works:
Code:
#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);
system("PAUSE");
}
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);
}
This works but it's not wat I was looking for, it was only a "try".
The REAL program it is the seguent (it uses the same funcion I created before) :
Code:
#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);
}
It looks in a file for the string I want and trys to modify it BUT it doesnt work because the output is :
prog=0.64155436
229339200.00%
and not
Thanx to everyone