Search code examples
maxscript

Maxscript / cant write string in middle of text file


Hi Guys i tried to add something to middle of a text file like this :

fs = openFile "D:\Projects\Exports\Map\info.txt" mode:"a+"
skipToString fs "line3"
skipToNextLine fs
print "Hello" to:fs
close fs

my text file is like this :

line1 line2 line3 line4 line5

and i want to add "hello" after line3 text. but it always add the text to the end of the file:

line1 line2 line3 line4 line5 "Hello"

anyone know what is the problem ? thanks


Solution

  • mode:"a+" will always append the file in the end of file. Try this:

    fs = openFile "D:\Projects\Exports\Map\info.txt" mode:"r+"
    skipToString fs "line3"
    format "Hello" to:fs
    close fs
    

    This will overwrite the characters after "line3", so you need to use substitutestring or something else. I changed print to format as print adds a new line after the "Hello". There is no need for skipToNextLine, it will position the cursor to next line in the file.