We wish now to discuss in more detail how to output data from Maple into a file suitable for reading by another program, and how the data should be formatted by the external program so that Maple can read it in.
In Maple V Release 2 the file I/O facilities now include the the routines printf, sscanf and readline. The routine printf is used for general formatted printing. It is modelled after the printf routine from C and is the same except that it also accepts the %aoption for algebraic output. Here is a typical example of using printf to print floating point data.
=-1.00.5plus##1`##1=12=^^M=12 > printf(`A float in fixed point: A float in fixed point: 0.577216 and scientific notation: 5.772157e-01
plusplus -100 plus The printf function takes a string as it's first argument which specifies text to be printed and how the values, which are given as additional arguments are to be printed using %control sequences. In our example, there are two values to be printed using the %fand %econtrol sequences. The escape sequence \n is used to print a new line. Other useful control sequences are %dand %sfor printing integers and strings, and also the control sequence %afor printing a Maple algebraic expression. See ?printf for other options.
The readline routine is used to read a single line of text from a text file as a string. The sscanf routine can be used to scan a string and extract any numbers. It is the inverse of printf. So to read in some data, one can read each line of a file using readline and for each line use sscanf to extract data. The utility routine readdata has been written to read files of text which contain columns of numerical data separated by blanks or tabs. The readdata routine returns a list of the data, or a list of lists of the data if more than one column is specified. Suppose the file foo looks like this
Here is the result of reading in the data. =-1.00.5plus##1`##1=12=^^M=12 > readlib(readdata): # load the readdata function from the library > readdata(foo,2); # read the first two columns of the data2.1 3.2 1.2 2.4 3.1 3.4 3.9 1.1 5.5
[[2.100000000000000, 3.200000000000000],
[2.400000000000000, 3.100000000000000],
[3.900000000000000, 1.100000000000000]]
plusplus -100 plus