Recherche Google
OpenMusic 6 – User ManualOM Objects > OM Basic Objects > Text File Tools > File-Box > Writing / Reading
previous pagenext page

Writing and Reading Files

StreamFile : a Pointer to Read and Write Files

Connections

The StreamFile box must be attached to a file via its pathname. It has one input and one output :

Actions and Formats

The properties of the StreamFile can be edited if needed.

To open the StreamFIle properties window, double click on the box.

  • Three options are offered a the top of the window : Read / Write, Write, Read.
  • The File Type pop up menu offers two file formats : text or SDIF.
File Options

The File Exists pop up menu offers four options, which apply to an existing targeted file.

Each of these four options determine the way the program behaves when the text file is open or created to access its contents.

This behaviour results from the position of the pointer and the choice of a destructive or non destructive action.

  • " Rename " : the existing file is renamed and a new file is created.
  • " Supersede " : the existing file is superseded; that is, a new file with the same name as the old one is created. The old file is deleted.
  • " Overwrite " : the file is modified according to the position of the file pointer. For instance, if an existing file contains ten lines, and that the new data represents three lines, the three first lines of the existing file are replaced.
  • " Append " : the new data is added after the ending of the previous content of the file.

Writing Contents

File-Write

The file-write function writes something in a file.

It has two inputs and one output:

  • "line" : the text or data to write in the file,
  • "stream" : a targeted file to write in, that is, the streamfile box within the File-Box.
  • Its output returns the text of the first input.
File-Write-Line

The file-write-line function is similar to file-write, but adds a line return at the end of the input data.

To add several lines at the end of the file, an iterative process must be introduced in the program. Most of the time, each iteration triggers one operation in the File-Box loop.

Write 1001 times in the file – loop from 0 to 1000
Write 1001 times in the file – loop from 0 to 1000

Reading Data from a File

File-Read-Line

The file-read-line function allows to read one line in a file.

It has one input and one output :

  • "stream" : a stream returned by streamfile.
  • Its output returns a text line.

When all the lines of the text are exhausted, file-read-line returns "nil". Lines can be read one by one.

Most of the time, each iteration triggers one operation. In this case, the successive lines can be stored with an accumulator such as collect, as in an OMLoop.

Reading and collecting the contents of the six first lines in a file – loop from 0 to 5.
Reading and collecting the contents of the six first lines in a file – loop from 0 to 5.
File-Eof-p

"Eof" stands for "end of file", "p" for "predicate".

The file-eof-p function is a predicate, which allows to read a whole file whose number of lines is unknown. This function tests if a streamfile is at the end of the file.

It has one input and one output :

  • "stream" : a stream returned by streamfile.
  • Its output returns "t" or "nil".

It can be associated with whileloop and OMIF, which execute a specific operation when the end of the file has been reached.

As long as the predicate finds no streamfile, it returns "nil", and lines are collected.
As long as the predicate finds no streamfile, it returns "nil", and lines are collected.
previous pagenext page
About...(c) Ircam - Centre PompidouMade with Scenari