Initializing help system before first use

Formated data output to file

Just like initializations from in the previous section, initializations to also exists in Mosel to write out data in a standardized format. However, if we wish to redirect to a file exactly the text that is currently displayed in the logging window of Workbench, then we simply need to surround the printing of this text by calls to the procedures fopen and fclose:

 fopen("result.dat", F_OUTPUT)
 writeln("Total return: ", getobjval)
 forall(s in SHARES) writeln(s, ": ", getsol(frac(s))*100, "%")
 fclose(F_OUTPUT)

The first argument of fopen is the name of the output file, the second indicates in which mode to open it: with the settings shown above, at every re-execution of the model the contents of the result file will be replaced. To append the new output to the existing file contents use:

 fopen("result.dat", F_OUTPUT+F_APPEND)

We may now also wish to format the output more nicely, for instance:

 forall(s in SHARES)
  writeln(strfmt(s,-12), ": \t", strfmt(getsol(frac(s))*100,5,2), "%")

The function strfmt indicates the minimum space reserved for printing a string or a number. A negative value for its second argument means left-justified printing. The optional third argument denotes the number of digits after the decimal point. With this formated way of printing the result file has the following contents:

Total return: 14.0667
treasury    : 	30.00%
hardware    : 	 0.00%
theater     : 	20.00%
telecom     : 	 0.00%
brewery     : 	 6.67%
highways    : 	30.00%
cars        : 	 0.00%
bank        : 	 0.00%
software    : 	13.33%
electronics : 	 0.00%