I am given two .txt files; first has some lines that contain a number and a word, the other has some text. I have to read the number n
and a word and write the word starting at position n
in the other file. The code I wrote just writes all the words to the end and I don't know how to fix it.
osAssert(3 == argc, "argc");
struct stat fInfo;
osAssert(-1 != stat(argv[1], &fInfo), "stat");
osAssert(S_ISREG(fInfo.st_mode), "prvi fajl nije reg");
osAssert(-1 != stat(argv[2], &fInfo), "stat");
osAssert(S_ISREG(fInfo.st_mode), "drugi fajl nije reg");
FILE *f1 = fopen(argv[1], "r");
osAssert(NULL != f1, "open");
FILE *f2 = fopen(argv[2], "a");
osAssert(NULL != f2, "open2");
rewind(f2);
int fd = fileno(f2);
int n;
char rec[MAX];
while(EOF != fscanf(f1, "%d %s", &n, rec)){
printf("%d %s ", n, rec);
osAssert(-1 != fseek(f2, n, SEEK_SET), "fseek");
osAssert(-1 != write(fd, rec, strlen(rec)), "write");
}
fclose(f1);
fclose(f2);
osAssert function just checks if the condition is true or false and stops the program if it isn't.