Using File variables


Some times is useful to have a convenient and fast way to access a file at binary level. We can access a file at binary level to:

CalcIt offers this ability using File Variables. Declaring a file variable we connect instantly to a file. Using the file variable in a way similar to array variables, we read or write to the file. Any assignment to a file variable immediately changes the file. We don't need to care for saving our data or when to close the file. This is CalcIt's responsibility.


Contents in this topic

Declaring a file variable

The way to declare a file variable is familiar. We use a variation of the SET command:

SET f=FILE('c:\test.dat',ReadOnly,InitialSize);

The two last parameters are optional. The ReadOnly parameter is by default TRUE and if omitted the file variable is read-only. This protects the file from accidental modifications. The InitialSize parameter has to do with the creation of a new file. Is always in bytes and if the requested file name is not found on the disk then a file of this InitialSize is created, filled with zeroes. InitialSize parameter has no effect if the file is found on the disk. If omitted and the file is not found then an error is produced.

We can use many times the SET=FILE syntax over the same variable and redefine it as many times as needed.

If the declaration (or redefinition) is successful then we are able to access the file using the declared variable.

Accessing a File

To access a part of the file we need an index and a type of what we want to store or read. Because of that the syntax (either for read or write) has the following form:


The Index parameter is a numeric (zero-based) value from the start of the file, in bytes. "Zero-based" means that the first byte in the file is accessed at index 0.

The type parameter is optional. If omitted then the default type is assumed. This is defined with the command FileRecDef. If FileRecDef has not change the default type the BYTE (atBYTE) is assumed.

Type parameter can take the following values:

To write a value we use the assignment operator as expected:


Additionally all alternative special case faster operators can be used: ++, --, +=, -=, &=. See CalcIt operators for more.

Configuring Record size

As we said before the Index is a numeric value in bytes and permit us to locate a desired part of the file for read or write. This can be changed. We can define a record size other than of 1. This is useful if the file is broken or we want to be broken in equal sized pieces. In such a case we use command FileRecDef. Lets start examining this command:

FileRecDef(f, RecSize, DefaultType);

This command configures behavior of file variable f. The last two parameters are optional. We can define any of them or omit any of them. Simply place the appropriate commas to denote omitted parameters as usual The default values of optional parameters are:

The RecSize parameter defines the record size. This can be any positive number. This number is used by the file variable to compute the final location in the file: FinalLocation=Index*RecSize.

Setting Default access type

No mystery on this. Just use the second parameter (DefaultType) of the FileRecDef command and one of the types described above. When we use the file variable to access the file and omit its second optional parameter, then the type defined here will be used.

Using Buffer variables to access Files

We can use Buffer variables to access the contents of a file in a more structural way. It is easy. We create a Buffer variable with the desired field structure and make it point to a specific part of a File:

set bf=BUFDEF(nme:atCHAR[30], Address:atCHAR[30], Amound:atINTEGER)ADDR(&fle(10));

In the above code Buffer variable bf is created with the field structure defined in BUFDEF which points to fle(10) where fle is a file variable. The & operator means "Address Of" in this syntax.

For more information see Using Buffer Variables topic.

Additional commands to handle files

There are some additional and useful commands  to handle a file:

  1. FileIns
  2. FileDel
  3. FileAdd
  4. FileToFile
  5. READFile
  6. ArrToFile
  7. Size
  8. FileRecs
  9. FileCompare
  10. FileCopyStr
  11. FileSearch
  12. SetFileSize