FICO
FICO Xpress Optimization Examples Repository
FICO Optimization Community FICO Xpress Optimization Home
Back to examples browserPrevious exampleNext example

Read two tables and a set from a text data file

Description
Read two tables and a set from a free format data file (text file).
  • opening and closing a file
  • skipping comments and empty lines
  • using 'read' and 'readln'
  • using loops: forall, repeat-until, while


Source Files
By clicking on a file name, a preview is opened at the bottom of this page.
readfile.mos[download]

Data Files





readfile.mos

(!*******************************************************
  * Mosel Example Problems                              *
  * ======================                              *
  *                                                     *
  * file readfile.mos                                   *
  * `````````````````                                   *
  * Example for the use of the Mosel language           *
  * (Read a data file)                                  *
  *                                                     *
  * (c) 2008 Fair Isaac Corporation                     *
  *     author: Y. Colombani, 2001                      *
  *******************************************************!)

model readfile

parameters
 F="Data/datafile.dat"
end-parameters

declarations
 t1,t2:integer
end-declarations

fopen(F,F_INPUT)            ! Open the file F for reading
write("Reading file `",F,"'...")

fskipline("#\n")            ~~COMMENT3~~
readln(t1,t2)               ! Read the size of the table on a line

declarations
 TF:array(1..t1,1..t2) of integer
 TD:array(string,string) of real
 S: set of string
 s1,s2:string
end-declarations

fskipline("#\n")            ~~COMMENT5~~
forall (i in 1..t1)
do
 forall (j in 1..t2)
  read(TF(i,j))             ! Read t2 values
 readln                     ! on each line
end-do

fskipline("#\n")            ~~COMMENT8~~
repeat
 readln(s1,'|',s2,'|',TD(s1,s2))    ! Read the indices and the value on a line
until getparam("nbread")<5  ! Until a line is not properly constructed

fskipline("#\n")            ~~COMMENT11~~
read(s1)                    ! Read a string
while (s1<>'.' and not iseof)
do                          ! While the string is not '.' and the end of file
                            ! is not reached
 S+={s1}                    ! Add the string to the set
 read(s1)                   ! Read another string
end-do

fclose(F_INPUT)
writeln(" Done.\n")

writeln("TF=",TF)
writeln("TD=",TD)
writeln("S =",S)
end-model


Back to examples browserPrevious exampleNext example