0

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.

Marijam
  • 5
  • 2
  • You are appending to the file (by using `"a"`), maybe see https://app.box.com/folder/222932152130? – doctorlove Aug 24 '23 at 12:54
  • 2
    Unrelated: `EOF != fscanf(f1, "%d %s", &n, rec)` is not good and could lead to infinite loops. Use `2 == fscanf(f1, "%d %s", &n, rec)` instead. – Some programmer dude Aug 24 '23 at 12:55
  • As for your problem, why not open the second file (`f2`) is plain write mode `"w"`? – Some programmer dude Aug 24 '23 at 12:56
  • Oh, and why are you using `write` to write to the unknown descriptor `fd`? How is this related to the file `f2`? And what happens if the string you want to add is of a different length than what's already there? There's just too many unknowns in your question and code to really be able to help you. – Some programmer dude Aug 24 '23 at 13:08
  • @Someprogrammerdude Opening a file with `"w"` will truncate the file to 0 and the text will be erased, I have to write the new word over the characters that are already in the second file. I have added the line that explains the origin of `fd`. – Marijam Aug 24 '23 at 17:39
  • You can't really replace text in a file, unless the new text is ***exactly*** the same length as the old. The simplest way to change a text file is to use temporary storage (memory or another file) and write the contents of the file to it, changing the things that needs to be changed. Then rewrite the original file from scratch. – Some programmer dude Aug 24 '23 at 17:42
  • @Marijam do you have to insert the text at the given position overwriting what's there, or do you have to shift over all the remaining parts of the target file? – Dario Petrillo Aug 25 '23 at 14:31
  • @DarioPetrillo I have to overwrite what's there. – Marijam Aug 26 '23 at 16:04

1 Answers1

0

If you want to overwrite a part of the file, leaving everything else as is, you can open the target file as "r+". This will open the file, without truncating it, and place the "cursor" at offset 0 with read and write capabilities.

Then, after fopen-ing the file, fseek to the position you want to write at and fwrite your data there.

Have a look at this diagram for all the possible opening modes (from this SO post): fopen() mode flowchart

Dario Petrillo
  • 980
  • 7
  • 15