11 March 2016

Lesson 16 – Using files in 8th – Part 1

Input and output

In order to permanently store data, we need a system to provide that possibility. Every full blown Operating System has got it’s own filesystem(s). E.g. in modern Windows you can use the NTFS file system and in Linux the ext2, ext3 and even Reiserfs file systems.

The file system is a layer between the OS and the hardware. The OS provides specific interfaces (API’s) for programmers to use such a file system.

Most programming languages encapsulate these API’s in their own programming language parlance.

Today we will start to learn how 8th does this.

It is very important to note that 8th is a Multi Platform development tool and that the file handling we do as a developer is on an abstract high level in such a way that 8th behaves correct even when using totally different file systems on totally different OS-es!

What is a file?

A file is a collection of sequentially stored records.

Mostly files are written and read sequentially (record by record). However, it is possible to point to a specific part of a file and read / write “at random”.

Furthermore, there is an important difference between 2 types of file:

  • Text files - such files are written line by line. Each line ends with (a combination of) CR (Carriage Return) and/or LF (Line Feed) characters (sometimes called newline characters
    • Example:   documents
  • Binary files - these files can contain anything. CR and LF characters that happen to be part of such a file have no special meaning
    • Example:   executable files, pictures (JPG), music (MP3), etc.

Files are also used as the building blocks of a database. But that is out of scope for us today.

Test if a given file does exist

If you are not sure if a file does exist, you can check for that:






If false is returned, the file does not exist (as in this case), otherwise true

Create a new file

Before we can write to a file, we must create that file, like so:


If the operation fails, the item on the stack will be null, otherwise it will be the file handle of the file just created.

Write to the new file

As we can see in the previous picture, f:create leaves the file handle on the stack, which is convenient for writing, because f:write expects that file handle to be on the stack!

Writing a line goes like so:



As we see on the stack there are now 2 items:

  • at TOS is the number of bytes written (here: 6) or null if the operation failed
  • at position 2 there is still the file handle (which is convenient for other writes)

Do not forget to check for null!

For now we drop the value on TOS:



As we can see the file handle is now at TOS again.

Now we write a second line:



As we can see the operation succeeded again and the file handle is still present in position 2 on the stack.

We drop the result again:



and see that the file handle is still present.

Close the file just written

In order to make the file correctly visible and complete in the file system of the OS, we must close the file:







The close word grabs the file handle from TOS and closes the file.

Maybe you would expect a return code from the close word. However, that would be meaningless, because we cannot “recover” anything if it failed!

Checking the file which we wrote

Using a text editor we now open the file just created:












We see that there is just one line written, with the text of both write operations. What the heck??? I wanted to write 2 text records, not this!!!

Binary file by default

In 8th by default files are written as binary files.

In our examples we didn’t provide a newline (CR of LF) character, which is needed in text files!

How to write a text file?

Well, that is easy! You should append a newline to your text to be written. Instead of:

  • “line 1”    f:write

You should use:

  • “line 1\n”    f:write

More info about 8th

8th Website:   http://8th-dev.com
8th Forum:   http://8th-dev.com/forum
My YouTube Channel:   https://www.youtube.com/channel/UCcwRyVDxU-lMfWgz_10DudA
The Big 8th Lessons Book:   http://goo.gl/qDa6fH

No comments:

Post a Comment