Advertisement
sp1d3o

mod_code_w\comp

Nov 4th, 2021
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.63 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <stdlib.h>
  4. #include <stdbool.h>
  5. #include <ctype.h>
  6.  
  7. #define MAX_W 1000
  8. #define MAX_F 500
  9.  
  10. void clear(void);
  11.  
  12. int main(int argc, char *argv[])
  13. {
  14.   FILE* rep = fopen("code_to_change.c", "r");
  15.   FILE* tmp = fopen("Temp.c", "w");
  16.  
  17.   int ch;
  18.   int chk;
  19.  
  20.   if(rep == NULL || tmp == NULL) {
  21.     perror("\nError");
  22.   }
  23.  
  24.   else {
  25.     char target[MAX_W] = "func";
  26.     clear;
  27.     char replace[MAX_F] = "printf";
  28.     clear;
  29.  
  30.     while(1) {
  31.       ch = fgetc(rep);
  32.       if(ch == EOF) {
  33.         break;
  34.       }  
  35.       else if(ch == ' ' || ch == '\t' || ch == '\n' || ch == '\r') {
  36.         fputc(ch, tmp);
  37.       }  
  38.       else {
  39.         char buffer[MAX_W];
  40.         fseek(rep, -1, SEEK_CUR);
  41.         fscanf(rep, "%500s", buffer);
  42.         if(strcmp(buffer, target) == 0) {
  43.           strcpy(buffer, replace);
  44.         }  
  45.         fprintf(tmp," %s", buffer);
  46.       }  
  47.     }  
  48.     fclose(rep);
  49.     chk = fclose(tmp);
  50.     if(chk == EOF) {
  51.       remove("Temp.c");
  52.       perror("\nFailed");
  53.     }
  54. fclose(rep);
  55.     chk = fclose(tmp);
  56.     if(chk == EOF) {
  57.       remove("Temp.c");
  58.       perror("\nFailed");
  59.     }  
  60.     else {
  61.       if(rename("Temp.c", "code_to_change.c") == 0) {
  62.         printf("Good\n");
  63.       }
  64.       else {
  65.         remove("code_to_change.c");
  66.         if(rename("Temp.c", "code_to_change.c") == 0) {
  67.           printf("Good\n");
  68.         }
  69.         else {
  70.           remove("Temp.c");
  71.           perror("\nFail");
  72.         }
  73.       }
  74.     }
  75.   }
  76.   return 0;
  77. }
  78.  
  79. void clear()
  80. {
  81.   int clear;
  82.  
  83.   while((clear = getchar()) != '\n' && clear != EOF);
  84. }
  85.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement