Advertisement
Guest User

Untitled

a guest
Jan 22nd, 2020
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.43 KB | None | 0 0
  1. #define _CRT_SECURE_NO_WARNINGS
  2. #include <string.h>
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5.  
  6. void load_n_work(char *name, char *substr, int pos);
  7.  
  8.  
  9. int main(int argc, char *argv[]) {
  10.     /*if (argc < 3) { printf("Not enought arguments\n"); system("pause"); exit(1); }
  11.     char *name = (char *)argv[1];
  12.     char *substr = (char *)argv[2];
  13.     int num = atoi(argv[3]);*/
  14.  
  15.     char *name = "name.txt";
  16.     char *substr = "(test)";
  17.     int pos = (atoi("5")-1); //позиция в файле для смещения
  18.     load_n_work(name, substr, pos);
  19.     system("pause");
  20.  
  21. }
  22.  
  23. void load_n_work(char * name, char * substr, int pos)
  24. {
  25.     FILE *fp;
  26.     if ((fp = fopen(name, "r+")) == NULL) { printf("error\n"); exit(1); }
  27.     int space = strlen(substr);
  28.     fseek(fp, -1, SEEK_END); //qwert|y
  29.     char ch;
  30.     int test = ftell(fp);
  31.     printf("In the beginning, pointer is on %d position\n", test);
  32.     int flag = 0;
  33.  
  34.     while (!flag) {
  35.    
  36.  
  37.         if (ftell(fp) == pos) flag = 1;
  38.         ch = fgetc(fp);  //qwert(y)|
  39.         test = ftell(fp);
  40.         printf("After grabbing a char, pointer is on %d position\n", test);
  41.  
  42.  
  43.         fseek(fp, space - 1, SEEK_CUR);     //(space = 5)    qwerty....|.
  44.         fputc(ch, fp);                      //(space = 5)    qwerty....y|
  45.         test = ftell(fp);
  46.         printf("After releasing a char, pointer is on %d position\n", test);
  47.         fseek(fp, -(space + 2), SEEK_CUR);  //(space = 5)    qwer|ty....y
  48.         //system("pause");
  49.  
  50.     }
  51.     fseek(fp, pos, SEEK_SET);
  52.     fputs(substr, fp);
  53.  
  54.     fclose(fp);
  55.  
  56.  
  57. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement