File Output Using Gforth

As a first project, I wrote a short program for rendering the Mandelbrot fractal. I need to try to output my results to a file (e.g. .bmp or .ppm) and get stuck.

I really did not find examples of what I'm trying to do, but I found two code examples for copying from one file to another.

The examples in the Gforth documentation (section 3.27) didn’t work for me (winXP), in fact they seemed to open and create files, but they didn’t write them to files correctly.

This is an example of the Gforth documentation that copies the contents of one file to another:

0 Value fd-in 0 Value fd-out : open-input ( addr u -- ) r/o open-file throw to fd-in ; : open-output ( addr u -- ) w/o create-file throw to fd-out ; s" foo.in" open-input s" foo.out" open-output : copy-file ( -- ) begin line-buffer max-line fd-in read-line throw while line-buffer swap fd-out write-line throw repeat ; 

I found this example ( http://rosettacode.org/wiki/File_IO#Forth ) that works. The main problem is that I cannot isolate the part that writes to the file, and it still works. The main confusion is that> r does not seem to consume TOS, as I could expect.

 : copy-file2 ( a1 n1 a2 n2 -- ) r/o open-file throw >r w/o create-file throw r> begin pad maxstring 2 pick read-file throw ?dup while pad swap 3 pick write-file throw repeat close-file throw close-file throw ; \ Invoke it like this: s" output.txt" s" input.txt" copy-file 

I would really appreciate it if someone could explain how open files work, create read and write words for a file, as my investigation leads to a few strange stacks.

Any clues as to why the Gforth examples do not work can also help.

In general, I want to output from Gforth to a file, and so far has been ripped off. Can anyone help?


Thanks Vijay , I think I understand the example you gave. However, when I try to use something like this (which I think seems to be):

 0 value test-file : write-test s" testfile.out" w/o create-file throw to test-file s" test text" test-file write-line ; 

I get ok , but nothing is placed in the file, did I make a mistake?


It seems that the problem was that the corresponding buffers were not deleted or the file was obviously closed. Adding something like

 test-file flush-file throw 

or

 test-file close-file throw 

between write-line and ; makes it work. Therefore, the Gforth documentation example would work if I followed the instructions.

Thanks again Vijay for the help.

+4
source share
1 answer

I will try to explain how write-line works with this simple example. Here we have a buffer containing the string "hello" and we want to write this to a file opened with open-output .

 buffer 5 fd-out write-line 

5 - buffer length. fd-out is an open file descriptor. Calling write-line will result in an integer result on the stack, the value of which depends on the implementation. More information on I / O files can be found in File Access Words .

Calling the word throw is optional. It will check the integer value at the top of the stack, and based on this value, it either pulls the topmost exception frame from the exception stack, or calls abort or displays an implementation-specific message that provides information about the state associated with the integer. (For exact details on how throw works, see THROW .)

+1
source

Source: https://habr.com/ru/post/1332632/


All Articles