将文本文件写入 Forth 上的数组 [英] Writing a text file into an array on Forth

查看:34
本文介绍了将文本文件写入 Forth 上的数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个文本文件,其中包含一组数字,例如:

I have a text file, containing an array of numbers such as:

1 0 0 1 0
0 1 1 0 1
0 0 0 1 1
1 1 0 0 0
0 0 1 0 0

我用以下代码打开了文本文件:

I have opened the text file with the following code:

variable file-id
: open_file ( -- ) \ Opens the text file
  s" c:\etc\textfile.txt" r/w open-file throw
  file-id ! ;

我还创建了一个数组来存储这些数据:

I have also created an array to store this data:

create an_array 25 chars allot \ Create the array

: reset_array ( -- ) big_array 25 0 fill ;
reset_array \ Set all elements to 0

有没有办法用Forth把文本文件的内容写入数组?

Is there a way to write the contents of the text file into the array with Forth?

推荐答案

1.懒人方式

一种懒惰的方法是通过对文件名执行 included 来评估文件.

\ Some library-level common words
\ --------------------------------

\ Store n chars from the stack into the array at c-addr, n >= 0
: c!n ( n*x n c-addr -- )
  >r begin dup while ( i*x i )
    \ store top from i*x into addr+(i-1)*char_size
    1- tuck chars r@ + c! ( j*x j )
  repeat drop rdrop
;

\ Execute xt and produce changing in the stack depth
: execute-balance ( i*x xt -- j*x n ) depth 1- >r execute depth r> - ;

: included-balance ( i*x c-addr u -- j*x n )
  ['] included execute-balance 2 +
;

\ The program
\ --------------------------------

create myarray 25 chars allot \ Create the array

: read-myfile-numbers ( -- i*x i )
  state @ abort" Error: only for interpretation"
  s" ./mynumbers.txt" included-balance
;
: write-myarray ( i*x i -- )
  dup 25 <> abort" Error: exactly 25 numbers are expected"
  myarray c!n
;
: fill-myarray-from-myfile ( -- )
  read-myfile-numbers write-myarray
;

2.整洁的方式

一个谨慎的方法是读取文件(完整或逐行),将文本拆分为词素,将词素转换为数字,然后将数字存储到数组中.

2. Tidy way

A careful way is to read a file (completely, or line by line), split the text into lexemes, convert lexemes into numbers, then store the numbers into your array.

请参阅:如何在 Forth 中输入数字.

在底层,它可以通过 read-fileread-line,类似于 word|tail>number(或类似上面示例中的 StoN 库词).

On a low level it can be done via read-file or read-line, something like word|tail and >number (or something like StoN library word from the example above).

在更高层次上:使用 Gforth 特定词,例如 execute-parsing执行解析文件parse-names>号码?

On a higher level: use Gforth specific words like execute-parsing or execute-parsing-file, parse-name and s>number?

: next-lexeme ( -- c-addr u|0 )
  begin parse-name ?dup if exit then ( addr )
    refill 0= if 0 exit then drop
  again
;
: read-myfile-numbers ( -- i*x i )
  s" ./mynumbers.txt" r/o open-file throw
  [:
    0 begin next-lexeme dup while
      s>number? 0= abort" Error: NaN" d>s swap 1+
    repeat 2drop
  ;] execute-parsing-file
;

如果需要读取的数字太多,必须一次一个地写入数组,而不是全部入栈.

If you need to read too many numbers, you have to write them into the array one by one at once, instead of placing all of them into the stack.

这篇关于将文本文件写入 Forth 上的数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆