How do I insert a string in a file on a SD card?

I’m working on a gps logger and I would like that it saves KML files. The problem with all the examples that I see, is that they append lines to the end of the file each gps update. But I want to insert lines at a specific point. Is this possible without completely rewrite the whole file?

For example, A kml file would look like this:

<?xml version="1.0" encoding="UTF-8"?>
<kml xmlns="http://www.opengis.net/kml/2.2">
  <Document>
    <name>Paths</name>
    <description>Examples of paths. Note that the tessellate tag is by default
      set to 0. If you want to create tessellated lines, they must be authored
      (or edited) directly in KML.</description>
    <Style id="yellowLineGreenPoly">
      <LineStyle>
        <color>7f00ffff</color>
        <width>4</width>
      </LineStyle>
      <PolyStyle>
        <color>7f00ff00</color>
      </PolyStyle>
    </Style>
    <Placemark>
      <name>Absolute Extruded</name>
      <description>Transparent green wall with yellow outlines</description>
      <styleUrl>#yellowLineGreenPoly</styleUrl>
      <LineString>
        <extrude>1</extrude>
        <tessellate>1</tessellate>
        <altitudeMode>absolute</altitudeMode>
        <coordinates> -112.2550785337791,36.07954952145647,2357
          -112.2549277039738,36.08117083492122,2357
          -112.2552505069063,36.08260761307279,2357
          -112.2564540158376,36.08395660588506,2357
          -112.2580238976449,36.08511401044813,2357
          -112.2595218489022,36.08584355239394,2357
          -112.2608216347552,36.08612634548589,2357
          -112.262073428656,36.08626019085147,2357
          -112.2633204928495,36.08621519860091,2357
          -112.2644963846444,36.08627897945274,2357
          -112.2656969554589,36.08649599090644,2357
          !!! I WANT TO INSERT HERE !!!
        </coordinates>
      </LineString>
    </Placemark>
  </Document>
</kml>

Now I want to insert new coordinates line before “”
Is this possible using the seek function? Just file.seek(filesize – bytes used for till
Followed by: file.println(new cordinates)?

In this case I trust that the last part is a fixed part of bytes. But what if I don’t know how many bytes there are after . Is there some kind of string search function in the SD library? And if so, will it be fast enough to do this a couple of times a minute?

You don't need to rewrite the whole file, but you need to rewrite everything from the point where you append the coordinates. When you add new coordinates, save the position after the last coordinates (where you want to write the next set of coordinates). On the next time you need to write the coordinates, you open the file for writing, seek to the position you saved and write the coordinates and the part of the file that comes after the coordinates (e.g, ... ")

So, let’s say the whole file is 1000 bytes. The last part (e.g, ... ") is 100 bytes and the new string with coordinates is 20 bytes.
If I seek to the part where I want to insert the new string, and I “file.println(new coordinates string)” and do nothing extra, the file will chopped off and the new total filesize is 920 bytes? (1000-100+20)
And, if for example I want the new coordinates before the previous coordinates… this won’t be possible (without writing a complete new file)?

I “file.println(new coordinates string)” and do nothing extra, the file will chopped off

No, it just overwrites what is at that position in the file.

Okay, thank you! It's clear to me now.

You don't actually have to save the write position as I earlier suggested. If the last part of the file is always the same, you can seek back a fixed length from the end of the file.