Search code examples
iogforth

How can I save input to a file?


I use gforth running on linux boxes.

For one of my mini-applications I want to register a formatted text output from a few different user inputs.

Here is the INPUT$ I use:

: INPUT$  
  pad swap accept pad swap ; 

I think this is correct. I tested it this way:

cr ." enter something : " 4 INPUT$ CR 
enter something : toto 
 ok
cr ." enter something : " 8 INPUT$ CR 
enter something : titi 
 ok
.S <4> 140296186274576 4 140296186274576 4  ok

My file definition:

256 Constant max-line
Create line-buffer  max-line 2 + allot
//prepare file for Write permissions :    
s" foo.out" w/o create-file throw Value fd-out
: close-output ( -- )  fd-out close-file throw ;

The end goal is to build very small files as:

data1;data2;data3
data4;data5;data6

where each data is the user input (asked 3times to insert text & a second wave of 3 inputs)

I did not find documentation about how I can use text inputs to build my file.

How can I call my stack data to copy them to the text file format? (using type will only echo texts to my terminal)


Solution

  • I think you are looking for the Forth write-file and write-line words, which are documented here: https://www.complang.tuwien.ac.at/forth/gforth/Docs-html/General-files.html

    write-file ( c-addr u fileid -– ior )
    
    write-line ( c-addr u fileid –- ior )
    

    Pass the address and length of your text buffer, and the file ID (fd-out in your example) to write text to the file. The ior result will be zero on success.