ItachiSan

Text Replace Function in C

Oct 8th, 2013
167
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.96 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <string.h>
  3. #define MAX_LEN_SINGLE_LINE     150
  4. void main(char argc[6])
  5. {  
  6.     printf("Version is %s\n", version);
  7.     const char fileOrig[32] = "work.xml";
  8.     const char fileRepl[32] = "work.xml";
  9.     const char text2find[80] = "<Version>000000</Version>";
  10.     const char text2repl[80] = "<Version>000001</Version>";
  11.     char buffer[MAX_LEN_SINGLE_LINE+2];
  12.     char *buff_ptr, *find_ptr;
  13.     FILE *fp1, *fp2;
  14.     size_t find_len = strlen(text2find);
  15.     fp1 = fopen(fileOrig,"r");
  16.     fp2 = fopen(fileRepl,"w");
  17.     while(fgets(buffer,MAX_LEN_SINGLE_LINE+2,fp1))
  18.     {
  19.         buff_ptr = buffer;
  20.         while ((find_ptr = strstr(buff_ptr,text2find)))
  21.         {
  22.             while(buff_ptr < find_ptr)
  23.                 fputc((int)*buff_ptr++,fp2);
  24.             fputs(text2repl,fp2);
  25.             buff_ptr += find_len;
  26.         }
  27.         fputs(buff_ptr,fp2);
  28.     }
  29.     fclose(fp2);
  30.     fclose(fp1);
  31.     printf("Done!\n");
  32. }
Advertisement
Add Comment
Please, Sign In to add comment