Previous Up Next

4.1.2  Writing output to a file

Xcas has a basic file I/O support in form of three commands: fopen, fprint and fclose. Note that reading from files is not supported.

The fopen command creates and opens a file to write into.

To use this, you need to associate it with a variable var:=fopen(filename) which use to refer to the file when printing to it.

The fprint command writes to a file.

The fclose command closes a previously opened file.

Example

To write contents to a file, you first need to open the file and associate it with a variable.

f:=fopen("bar")

This creates a file named “bar” (and so erase it if it already exists). To write to the file:

x:=9:; fprint(f,"x+1 is ",x+1)

This puts

"x+1 is "10

in the file. Note that the quotation marks are not inserted if you use the Unquoted argument:

x:=9:; fprint(f,Unquoted,"x+1 is ",x+1)

This puts

x+1 is 10

in the file. Finally, after you have finished writing what you want into the file, you close the file with the fclose command:

fclose(f)

This returns 1 on success and 0 on failure.


Previous Up Next