Writing to a File in Maple R6

Maple has fprintf statements which work very similar to those of C.

This worksheet illustrates opening a file (potentially one whose

name is computed in a program) and writing some simple text and

numbers to the file.

These first 3 variables specify the location and name of the file being saved.

> DIR_LOCN :=`/tmp`;

> NUM:= 25;

> group_suffix := `grp`;

Here || is the string concatenation operator and `` is the empty string.

> group_output_file_path := ``|| DIR_LOCN || `/` || nm || NUM || `.` || group_suffix;

The format specifications for integers, floats, and algebraic expressions
are illustrated here. Many more exist. Also \t is a tab and \n a newline.

> fd := fopen(group_output_file_path,WRITE);
for i from 1 to 3 do
fprintf(fd,`An integer: %d\n`,i);
fprintf(fd,`\tA float: %g\n`,evalf(log(i+1)));
fprintf(fd,`\t\tAlgebra: %a\n`,expand((x+y)^i));
od:
fclose(fd);

Especially when debugging it is useful to have your output go to

the current worksheet instead of a file. Setting fd to default does this.

> fd := default;
for i from 1 to 3 do
fprintf(fd,`An integer: %d\n`,i);
fprintf(fd,`\tA Float: %g\n`,evalf(log(i+1)));
fprintf(fd,`\t\tAlgebra: %a\n`,expand((x+y)^i));
od:
#fclose(fd);

`An integer: 1`

`	A Float: .693147`

`		Algebra: x+y`

`An integer: 2`

`	A Float: 1.098612`

`		Algebra: x^2+2*x*y+y^2`

`An integer: 3`

`	A Float: 1.386294`

`		Algebra: x^3+3*x^2*y+3*x*y^2+y^3`

>