Thread: fprintf file problem

  1. #1
    Registered User ch4's Avatar
    Join Date
    Jan 2007
    Posts
    154

    fprintf file problem

    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 &#37;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 ??

  2. #2
    and the hat of copycat stevesmithx's Avatar
    Join Date
    Sep 2007
    Posts
    587
    I get the expected output.
    What compiler are you using?
    Edit:
    I am using Mingw port of GCC in Windows XP.
    Last edited by stevesmithx; 11-30-2008 at 07:52 AM.
    Not everything that can be counted counts, and not everything that counts can be counted
    - Albert Einstein.


    No programming language is perfect. There is not even a single best language; there are only languages well suited or perhaps poorly suited for particular purposes.
    - Herbert Mayer

  3. #3
    Registered User ch4's Avatar
    Join Date
    Jan 2007
    Posts
    154
    Quote Originally Posted by stevesmithx View Post
    I get the expected output.
    What compiler are you using?
    Edit:
    I am using Mingw port of GCC in Windows XP.
    I used in the first pc DevC++4.9.9.2 on Xp and Gcc on Linux(third pc) but the second pc(WinXP) had not the expected output.

    In reality i have no problem with that but i need an explanation. (The other pc is not mine)
    Last edited by ch4; 11-30-2008 at 09:07 AM.

  4. #4
    Registered User ch4's Avatar
    Join Date
    Jan 2007
    Posts
    154
    I suppose the fault belongs to the OS of the 2nd pc (WinXP).

  5. #5
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Code:
            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)
    You don't NULL-terminate tag, so strcmp() probably won't do what you expect. Oh, and if you reach the end of the file in that while loop, you'll print an EOF character to the output file, which should probably be avoided.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  6. #6
    Registered User ch4's Avatar
    Join Date
    Jan 2007
    Posts
    154
    Thanks i haven't noticed that.

  7. #7
    Registered User ch4's Avatar
    Join Date
    Jan 2007
    Posts
    154
    I found the problem. (in red)

    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);
    
              if(ch==EOF)
                 break;  
    
              putc(ch,xmlOUT);
              tag[i++]=ch;
              
              if(i>=length-1)
                 break;
            }
            tag[i]='\0';
    
            if(strcmp(tag,"<tilefono>\0")==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;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A development process
    By Noir in forum C Programming
    Replies: 37
    Last Post: 07-10-2011, 10:39 PM
  2. Problem with file and array
    By paok in forum C Programming
    Replies: 5
    Last Post: 05-01-2008, 04:19 AM
  3. help with text input
    By Alphawaves in forum C Programming
    Replies: 8
    Last Post: 04-08-2007, 04:54 PM
  4. C++ std routines
    By siavoshkc in forum C++ Programming
    Replies: 33
    Last Post: 07-28-2006, 12:13 AM
  5. Replies: 3
    Last Post: 03-04-2005, 02:46 PM