string - Overwriting the first N bytes in a file in Swift -


i'm trying replace first n bytes in file in swift own data, leaving rest of file unchanged, e.g. have string "oops", file (of length) contains look, daisy, , want contain oops, daisy. built-in functions i've found don't want:

try "oops".write(to: path, atomically: false, encoding: string.encoding.utf8) 

replaces entire file,

let outputstream = outputstream(url: outputurl, append: false) outputstream.write("oops", maxlength: 4) 

behaves same way, , setting append true appends text end of file. there easy way behavior want?

use filehandle.

let handle = filehandle(forwritingto: outputurl) handle.seek(tofileoffset: 0) handle.write("oops".data(using: .utf8)) handle.closefile() 

i leave reader deal handling optionals , needing catch errors.


Comments