Check this code
Code:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
int main(void)
{
int length=strlen("<tilefono>")+1;
int i=0;
char *tag;
char ch;
FILE *xmlIN;
FILE *xmlOUT;
tag=(char *)malloc(length*sizeof(char));
if(tag==NULL)
{
fprintf(stderr,"Allocation Error\n");
exit(-1);
}
xmlIN = fopen("A.xml","rb");
if(xmlIN==NULL)
{
fprintf(stderr,"Cannot open %s\n","A.xml");
exit(-2);
}
xmlOUT = fopen("B.xml","wb");
if(xmlIN==NULL)
{
fprintf(stderr,"Cannot open %s\n","B.xml");
exit(-2);
}
while(ch!=EOF)
{
i=0;
ch=getc(xmlIN);
if(ch!=EOF)
putc(ch,xmlOUT);
if(ch=='<')
{
tag[i++]=ch;
while(ch!='>')
{
ch=getc(xmlIN);
putc(ch,xmlOUT);
tag[i++]=ch;
if((ch==EOF)||(i>=length-1))
break;
}
if(strcmp(tag,"<tilefono>")==0)
{
ch=getc(xmlIN);
while(!isdigit(ch))
{
putc(ch,xmlOUT);
ch=getc(xmlIN);
}
fprintf(xmlOUT,"0030");
putc(ch,xmlOUT);
}
}
}
fclose(xmlIN);
fclose(xmlOUT);
free(tag);
printf("\n\t\t\tJob Done !\n");
return 0;
}
The above code add the 0030 prefix between <tilefono> tag from A.xml and the result is B.xml which is a copy of A including the 0030 prefix after<tilefono> tag.
I tried the code on Windows Xp / Linux and worked fine.
But i tried it on an another pc (Windows Xp) and fprintf didn't work. Every element copied exept the string 0030.
ex.
A.xml
<?xml version='1.0'?>
<!-- askhsh 2 -->
<name>
<tilefono>
2104112345
</tilefono>
</name>
B.xml (worked fine on Xp/Linux)
<?xml version='1.0'?>
<!-- askhsh 2 -->
<name>
<tilefono>
00302104112345
</tilefono>
</name>
B.xml (fprintf problem on another pc using XP) No prefix !!!
<?xml version='1.0'?>
<!-- askhsh 2 -->
<name>
<tilefono>
2104112345
</tilefono>
</name>
Is there any chance to happen this fault ????
Did i do something wrong ??