Handling of input/output
At the start of the execution of a program/model, three text streams are created automatically: the standard input, output and error streams. The standard output stream is used by the procedures writing text (write, writeln, fflush). The standard input stream is used by the procedures reading text (read, readln, fskipline). The standard error stream is the destination of error messages reported by Mosel during its execution. These streams are inherited from the environment in which Mosel is being run: usually using an output procedure implies printing something to the console and using an input procedure implies expecting something to be typed by the user.
The procedures fopen and fclose make it possible to associate text files to the input, output and error streams: in this case the IO functions can be used to read from or write to files. Note that when a file is opened, it is automatically made the active input, output or error stream (according to its opening status) but the file that was previously assigned to the corresponding stream remains open. It is however possible to switch between different open files using the procedure fselect in combination with the function getfid.
model "test IO" def_out:=getfid(F_OUTPUT) ! Save file ID of default output fopen("mylog.txt",F_OUTPUT) ! Switch output to 'mylog.txt' my_out:=getfid(F_OUTPUT) ! Save ID of current output stream repeat fselect(def_out) ! Select default ouput... write("Text? ") ! ...to print a message text:='' readln(text) ! Read a string from the default input fselect(my_out) ! Select the file 'mylog.txt' writeln(text) ! Write the string into the file until text='' fclose(F_OUTPUT) ! Close current output (='mylog.txt') writeln("Finished!") ! Display message to default output end-model