How do I write (append) a string to a file?

I would like to a open a file and append some strings to it. What is the command to open a file and write a String to it? Is it possible to keep the file open and write several string sequentially? Or is it possible to just open a file for appending a string?
Use Open/A to open a file to append to it.
Use fprintf with the file reference number from the Open operation to write to the file.
Use Close to close the file when you're done.

John Weeks
WaveMetrics, Inc.
support@wavemetrics.com
Function/S CreateAFile()
    Variable refnum
    Open refnum
    if (refnum)
        fprintf refnum, "pi = %g\r", pi
    endif
    FStatus refnum
    String filepath = S_path+S_fileName
    Close refnum
    return filepath
end

Function AddToFile(String filepath)
    Variable refnum
    Open/A refnum as filepath
    if (refnum)
        fprintf refnum, "e = %g\r", e
    endif
    Close refnum
end

Function Test()
    String fpath = CreateAFile()
    AddToFile(fpath)
end

The first function creates a new file, writes one line into it, and returns the full path to that file. The second function takes in a string containing a full path to a file, and adds a line to that file. The function test() calls both of those functions in sequence to create a file with two lines.

Clearly an inefficient way to make a two-line file! But it does illustrate the technique. When you invoke test(), a dialog is put up asking you to give a name to the file and choose a location for it. The function should complete without asking you for more information, since the full path to the file is returned from CreateAFile().

The background is that an open file has a read/write position associated with it in Igor Pro. You can query the file position via FStatus. And also set it via FSetPos. And by using Open/A that file position is set to the end internally, whereas Open just leaves it at the start.

I would say that if you want to write a string to a text file, fprintf is your best option. Just do 'fprintf refnum, mystring' and you should be good to go. I don't know if there is an intrinsic character limit, but I see no problem to 'dump' an entire string. And even if there was a character limit, its just a matter of splitting up the string into parts.