Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #define _CRT_SECURE_NO_WARNINGS
- #include <string.h>
- #include <stdio.h>
- #include <stdlib.h>
- void load_n_work(char *name, char *substr, int pos);
- int main(int argc, char *argv[]) {
- /*if (argc < 3) { printf("Not enought arguments\n"); system("pause"); exit(1); }
- char *name = (char *)argv[1];
- char *substr = (char *)argv[2];
- int num = atoi(argv[3]);*/
- char *name = "name.txt";
- char *substr = "(test)";
- int pos = (atoi("5")-1); //позиция в файле для смещения
- load_n_work(name, substr, pos);
- system("pause");
- }
- void load_n_work(char * name, char * substr, int pos)
- {
- FILE *fp;
- if ((fp = fopen(name, "r+")) == NULL) { printf("error\n"); exit(1); }
- int space = strlen(substr);
- fseek(fp, -1, SEEK_END); //qwert|y
- char ch;
- int test = ftell(fp);
- printf("In the beginning, pointer is on %d position\n", test);
- int flag = 0;
- while (!flag) {
- if (ftell(fp) == pos) flag = 1;
- ch = fgetc(fp); //qwert(y)|
- test = ftell(fp);
- printf("After grabbing a char, pointer is on %d position\n", test);
- fseek(fp, space - 1, SEEK_CUR); //(space = 5) qwerty....|.
- fputc(ch, fp); //(space = 5) qwerty....y|
- test = ftell(fp);
- printf("After releasing a char, pointer is on %d position\n", test);
- fseek(fp, -(space + 2), SEEK_CUR); //(space = 5) qwer|ty....y
- //system("pause");
- }
- fseek(fp, pos, SEEK_SET);
- fputs(substr, fp);
- fclose(fp);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement