Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Text file support
#3
(01-07-2022, 03:51 PM)Marcus Wrote:
(01-07-2022, 03:21 PM)kcfb Wrote: Can I ask if the above has been implemented in N7 at this stage? I have been trying to open/read/write and close text files, but cannot see how to do it? Many thanks.
ps - hope you are in good health Marcus, and you are clear of covid.

Sorry, here's an example:

Code:
' Create a file.
f = createfile("myfile.txt")
' If the file couldn't be created f will be an "unset" variable (sort of like
' null or nil in other languages). There are a couple of ways to check if it's
' valid:
'    if f <> unset
'    if typeof(f) <> TYPE_UNSET
'    if typeof(f)
' The last one works because the TYPE_UNSET constant is 0.
if typeof(f)
    ' Use wln to write something with a line break.
    wln file f, "This is a line with words!"
    ' Use write to write something without adding a line break.
    write file f, "This will become "
    wln file f, "another line"
    ' Close the file.
    free file f
endif

' Open a file.
f = openfile("myfile.txt")
if typeof(f)
    ' Read a whole line with frln.
    wln frln(f)
    ' Read word by word with fread.
    w = fread(f)
    ' frln, fread and freadc return an unset variable if end of file was
    ' reached.
    while typeof(w)
        wln w
        w = fread(f)
    wend
    free file f
endif

' I miss 'wait keydown' :)
system("pause")

You can also do it more n6:ish:

Code:
create file 0, "myfile2.txt"
if file(0)
    wln file 0, "This is a line with words!"
    write file 0, "This will become "
    wln file 0, "another line"
    free file 0
endif

open file 0, "myfile2.txt"
if file(0)
    wln frln(0)
    w = fread(0)
    while typeof(w)
        wln w
        w = fread(0)
    wend
    free file 0
endif

system("pause")


I don't show 'freadc' (for reading a single character) here, but I've attached an extremely unfinished reference document to the post where you can read about it. It may contain errors.
Fantastic - many thanks......
Reply


Messages In This Thread
Text file support - by kcfb - 01-07-2022, 03:21 PM
RE: Text file support - by Marcus - 01-07-2022, 03:51 PM
RE: Text file support - by kcfb - 01-07-2022, 04:05 PM

Forum Jump:


Users browsing this thread: 1 Guest(s)