如何使用Scheme附加到文件?

时间:2022-05-13 00:09:41

I am using TinyScheme (actually Script-Fu in GIMP), and cannot find a good way to open a file and append a line of text to it.

我正在使用TinyScheme(实际上是GIMP中的Script-Fu),并且找不到打开文件并向其附加一行文本的好方法。

I am trying to log some info to the file for debugging, and transcript-on doesn't seem to be implemented...

我正在尝试将一些信息记录到文件中进行调试,并且似乎没有实现transcript-on ...

Right now I am scraping along by reading the entire file (one character at a time!), concatenating that into a string, concatenating my text to the end of that, then writing it all out to the file again.

现在我正在阅读整个文件(一次一个字符!),将其连接成一个字符串,将我的文本连接到结尾,然后再将所有文件写入文件。

There must be a better way!

肯定有更好的办法!

2 个解决方案

#1


It's going to be something like

它会是这样的

(open-file "myfile" (file-options append))

You want to look up the file-options function.

您想要查找文件选项功能。

Update

Here's the guile version:

这是诡计版本:

(open-file "myfilename.dat" "a")

#2


Just had the same exact problem in GIMP and decided to use open-input-output-file. My solution is something like this:

刚刚在GIMP中遇到了同样的问题,并决定使用open-input-output-file。我的解决方案是这样的:

(let* ((out (open-input-output-file "file.txt") ))
  (display "hello world" out)
  (newline out)                    
  (close-output-port out))

Went through the TinyScheme source and checked that this function actually calls fopen with "a+". The file is open for reading and writing. For our purposes we only want to write, so just ignore reading.

通过TinyScheme源并检查此函数实际上是用“a +”调用fopen。该文件是开放的阅读和写作。出于我们的目的,我们只想写,所以只是忽略阅读。

I am writing a Script-Fu to write the values of gimp-selection-bounds to a file.

我正在编写一个Script-Fu来将gimp-selection-bounds的值写入文件。

#1


It's going to be something like

它会是这样的

(open-file "myfile" (file-options append))

You want to look up the file-options function.

您想要查找文件选项功能。

Update

Here's the guile version:

这是诡计版本:

(open-file "myfilename.dat" "a")

#2


Just had the same exact problem in GIMP and decided to use open-input-output-file. My solution is something like this:

刚刚在GIMP中遇到了同样的问题,并决定使用open-input-output-file。我的解决方案是这样的:

(let* ((out (open-input-output-file "file.txt") ))
  (display "hello world" out)
  (newline out)                    
  (close-output-port out))

Went through the TinyScheme source and checked that this function actually calls fopen with "a+". The file is open for reading and writing. For our purposes we only want to write, so just ignore reading.

通过TinyScheme源并检查此函数实际上是用“a +”调用fopen。该文件是开放的阅读和写作。出于我们的目的,我们只想写,所以只是忽略阅读。

I am writing a Script-Fu to write the values of gimp-selection-bounds to a file.

我正在编写一个Script-Fu来将gimp-selection-bounds的值写入文件。