1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
| void ChangerKeys(char *Thefilestr, unsigned int nbarg, ...)
{
FILE *Thefile,*tmpfile;
va_list adpar;
int i,c;
unsigned int TestDefine;
double keyvalue_arg,valuetmp;
char keyname_arg[10],strtmp[10];
va_start(adpar,nbarg);
Thefile = fopen(Thefilestr,"r+");
tmpfile = fopen("tmpfile","w");
if ((Thefile == NULL) || (tmpfile == NULL))
{
printf("Erreur le fichier est introuvable ou increable");
abort();
}
for ( i = 0 ; i < nbarg ; i++)
{
keyvalue_arg = va_arg(adpar,double);//Recupere la valeur de la clef
strcpy(keyname_arg,va_arg(adpar,char *));//Recupere le nom de la clef
do {
TestDefine = 0;
while ((TestDefine != 2 ) &&((c=fgetc(Thefile)) != EOF))
{
fprintf(tmpfile,"%c",c);
if (c =='#')
{
TestDefine = 1;
}
if ((c == 'd') && (TestDefine)) TestDefine = 2;
}
if (c == EOF)//Si on a atteind la fin du fichier, c'est pas normal
{
printf("\n\nErreur lors de la recherche de clef %s, fin de fichier atteinte.\n\n",keyname_arg);
abort();
}
fprintf(tmpfile,"efine ");
fseek(Thefile,6 * sizeof(char),SEEK_CUR);
//La position du fichier est maintenant après le #define
fscanf(Thefile,"%s",strtmp);//Lecture de la clef
fprintf(tmpfile,"%s",strtmp);
} while (strcmp(strtmp,keyname_arg) != 0);
//On a trouve la bonne clef (strtmp)
fscanf(Thefile,"%f",&valuetmp);
fprintf(tmpfile," %lf",keyvalue_arg);
}
while ((c=fgetc(Thefile)) != EOF)
{
fprintf(tmpfile,"%c",c);
}
rename("tmpfile",Thefilestr);
fclose(tmpfile);
fclose(Thefile);
} |
Partager