How do I write (append) a string to a file?
schollz
Thu, 04/27/2017 - 03:58 pm
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 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
April 28, 2017 at 08:59 am - Permalink
April 28, 2017 at 10:35 am - Permalink
In reply to by johnweeks
Could you provide an example?
January 10, 2022 at 03:11 pm - Permalink
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().
January 10, 2022 at 03:36 pm - Permalink
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.
January 11, 2022 at 05:16 am - Permalink
In reply to Function/S CreateAFile() … by johnweeks
Thank you John. This example is helpful. Probably the "fprintf" is not what I need in my code, as I am looking to 'dump' an entire string into a text file.
January 11, 2022 at 06:13 am - Permalink
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.
January 11, 2022 at 06:24 am - Permalink
In reply to I would say that if you want… by chozo
There seems to be a limit of 2400 bytes. The string I am trying to save is 100x larger. :(
January 11, 2022 at 06:28 am - Permalink
then probably you will want to use fbinwrite
January 11, 2022 at 06:41 am - Permalink
In reply to then probably you will want… by tony
Thank you Tony. That solved my issue.
January 11, 2022 at 06:44 am - Permalink