Magma provides a special file type for the reading and writing of external files. Most of the standard C library functions can be applied to such files to manipulate them.
Given a filename (string) S, together with a type indicator T, open the file named by S and return a Magma file object associated with it. Tilde expansion is performed on S. The standard C library function fopen() is used, so the possible characters allowed in T are the same as those allowed for that function in the current operating system, and have the same interpretation. Thus one should give the value "r" for T to open the file for reading, and give the value "w" for T to open the file for writing, etc. (Note that in the PC version of Magma, the character "b" should also be included in T if the file is desired to be opened in binary mode.) Once a file object is created, various I/O operations can be performed on it --- see below. A file is closed by deleting it (i.e. by use of the delete statement or by reassigning the variable associated with the file); there is no Fclose function. This ensures that the file is not closed while there are still multiple references to it. (The function is called Open instead of Fopen to follow Perl-style conventions. The following functions also follow such conventions where possible.)
Given a shell command line S, together with a type indicator T, open a pipe between the Magma process and the command to be executed. The standard C library function popen() is used, so the possible characters allowed in T are the same as those allowed for that function in the current operating system, and have the same interpretation. Thus one should give the value "r" for T so that Magma can read the output from the command, and give the value "w" for T so that Magma can write into the input of the command. See the Pipe intrinsic for a method for sending input to, and receiving output from, a single command.
Given a file F, flush the buffer of F.
Given a file F, return ftell(F), i.e. the position of the current offset in bytes within F.
(Procedure.) Perform fseek(F, o, p); i.e. move the file pointer of F to offset o (relative to p: 0 means beginning, 1 means current, 2 means end).
(Procedure.) Perform rewind(F); i.e. move the file pointer of F to the beginning.
(Procedure.) Put (write) the characters of the string S to the file F.
(Procedure.) Put (write) the characters of the string S, followed by a newline character, to the file F.
Given a file F, get and return one more character from file F as a string. If F is at end of file, a special EOF marker string is returned; the function IsEof should be applied to the character to test for end of file. (Thus the only way to loop over a file character by character is to get each character and test whether it is the EOF marker before processing it.)
Given a file F, get and return one more line from file F as a string. The newline character is removed before the string is returned. If F is at end of file, a special EOF marker string is returned; the function IsEof should be applied to the string to test for end of file.
Given a string S, return whether S is the special EOF marker.
Given a character (length one string) C, together with a file F, perform ungetc(C, F); i.e. push the character C back into the input buffer of F.
> function LineCount(F) > FP := Open(F, "r"); > c := 0; > while true do > s := Gets(FP); > if IsEof(s) then > break; > end if; > c +:= 1; > end while; > return c; > end function; > LineCount("/etc/passwd"); 59
> function GetTime() > D := POpen("date", "r"); > date := Gets(D); > _, _, f := Regexp("([0-9][0-9]):([0-9][0-9]):([0-9][0-9])", date); > h, m, s := Explode(f); > return h, m, s; > end function; > h, m, s := GetTime(); > h, m, s; 14 30 01 > h, m, s := GetTime(); > h, m, s; 14 30 04
Function that returns the contents of the text-file with name indicated by the string F. Here F may be an expression returning a string.
> Read("mystery.c"); #include <stdio.h>main(argc, argv) int argc; char **argv; { int n, i;
n = atoi(argv[1]);
for (i = 1; i <= n; i++) printf("%d\n", i * i);
return 0; } > System("cc mystery.c -o mystery"); > mysteryMagma := function(n) > System("./mystery " cat IntegerToString(n) cat " >outfile"); > output := Read("outfile"); > return StringToIntegerSequence(output); > end function; > mysteryMagma(5); [ 1, 4, 9, 16, 25 ]