Chomik: Reading Integers from a File


In this article, we will introduce file reading in Chomik. For now, we’ll keep things simple:
we will only talk about text files,
and we will only read integers stored in them.
This is enough to demonstrate the basic file stream mechanism in Chomik, which is consistent with many other parts of the language.
Creating a File Stream
In order to read from a file, you first need to create a file stream.
This is done with the built-in family of code variables called create
. When you call it, Chomik automatically updates a predefined integer variable named the created stream index
.
You can then store this index in your own variable. This is important because you might have multiple streams open at once, and you’ll need to keep track of which is which.
This “create-and-store” pattern is very common in Chomik.
Reading from a Stream
To read an integer from a stream, you use the built-in family of code variables called read from stream "integer"
.
As usual in Chomik, this command only executes the operation. To actually retrieve the value, you must evaluate the predefined integer variable the read from stream result "integer"
.
Another common Chomik pattern: code performs an action, variables hold the result.
Choosing Which Stream to Read
You might have multiple file streams open, so you must specify which one you want to read from.
You do this by setting the integer variable the read from stream source stream index
to the index of the stream you want to use.
Only then should you call read from stream "integer"
.
Example: Reading a Single Integer
Let’s put it all together.
The following program opens the file data.txt
, reads a single integer, and prints it:
<create new input filestream "data.txt">;
let my test filestream index = <the created stream index>;
let the read from stream source stream index = <my test filestream index>;
<read from stream "integer">;
let my test number = <the read from stream result "integer">;
<print "my test number=" <my test number>>;
What Happens Here?
Create the file stream for data.txt.
Store its index in my test filestream index.
Tell Chomik to read from that stream by setting the read from stream source stream index.
Perform the read operation.
Retrieve the integer from the
read from stream result "integer"
.Print the result.
Summary
File handling in Chomik follows the same philosophy you’ve seen before:
create something,
store the reference (index),
perform an action,
then retrieve the result from a variable.
In this article, we’ve learned how to open a file and read a single integer.
Subscribe to my newsletter
Read articles from Pawel Biernacki directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by

Pawel Biernacki
Pawel Biernacki
I was born in 1973, in Cracow, Poland. I am a software engineer.