0

In C, I write one line of data in a file and then another, and then I want to go back to the end of the first line and write without affecting the rest of the data.Is there any methods to write at the end of a line directly without copy the line first.

i write two line as follows. such as: line1:abc, line2:def,

then, i want to go back to the end of the line1 to write new data. line1:abc,abc, line2:def,

the new data wouldn't affect other data.

Leaf
  • 3
  • 1
  • 1
    Does this answer your question? [C function to insert text at particular location in file without over-writing the existing text](https://stackoverflow.com/questions/9033060/c-function-to-insert-text-at-particular-location-in-file-without-over-writing-th) – Matt Pitkin Apr 24 '23 at 08:11
  • 1
    if you're not going to change the line length (otherwise you'd need to _push_ the older content) then you could `fseek()` – Adriano Repetti Apr 24 '23 at 08:11
  • 2
    There are no insert-functions for file operations. New data will overwrite what is already in the file. The usual way is to write a new file, and rename it when completed. – BoP Apr 24 '23 at 08:12
  • SQL servers implement complex index trees to allow insertion of records. And you expect to be able to do this with simple function. – i486 Apr 24 '23 at 09:21

2 Answers2

1

There is no function to insert new content while preserving the rest of the file.

Use fseek() to reposition the stream then, as you said, copy the rest of the file, write the new content followed by the rest of the file.

The best option, however, is probably to create a new file by writing the first line, new content and then the rest of file. Then rename the new file to the old name.

Alternatively, make the changes in memory (making space with memmove()) and once the changes are done write the changed content to file. There are more advanced data structures (gap buffer) if you need to make lots of changes.

Allan Wind
  • 23,068
  • 5
  • 28
  • 38
  • Unless the lines length are constant, this will not work. Please, update your answer accordingly to avoid confusing the OP. – Tarik Apr 24 '23 at 08:32
  • @Tarik The way I read it is that op want to insert a new content at the end of line 1. The new content isn't "" otherwise it's a noop. The overwrite one string with another of the same length doesn't apply. If I am missing something let me know. – Allan Wind Apr 24 '23 at 08:47
  • @Allan Wind, I mean that I want to write something new at the end of a line.Such as the content of a line is "abc,", and i want to insert "abc," to the end of this line.It will be "abc,abc,", and the new content can't overwrite the next line. Based on the fact that I can get the offset of the end of this line through ftell (), whether i can complete this requirment. – Leaf Apr 24 '23 at 10:24
0

You cannot insert nor delete characters in between text lines. To achieve the requested effect, you would have to either rewrite the entire file or shift all of the data from the point of insertion by the number of characters that you need to insert or delete. If you do it in one go, you read everything from the point of insertion/deletion onward, seek to that position + number of additional characters or - number of deleted characters then write what was read. If you want to do it in chunks, then in case of insertion, you should read n characters at a time from the end of the file and write back shifted by +m characters (m being the number of extra characters). In case you are deleting character, you read n characters at a time starting from the insertion point and write them back shifted by -m characters, (m being the number of deleted characters). In case of character deletion, the file should be truncated by the number of deleted characters.

Note: No point in doing this unless you are dealing with huge data files that happen to be stored in text format. We are talking about 10's of gigs that do not fit in memory.

Tarik
  • 10,810
  • 2
  • 26
  • 40