Read a line from a file.
Integer_Type fgets (SLang_Ref_Type ref, File_Type fp)
fgets
reads a line from the open file specified by fp
and places the characters in the variable whose reference is
specified by ref
.
It returns -1
if fp
is not associated with an open file
or an attempt was made to read at the end the file; otherwise, it
returns the number of characters read.
The following example returns the lines of a file via a linked list:
define read_file (file)
{
variable buf, fp, root, tail;
variable list_type = struct { text, next };
root = NULL;
fp = fopen(file, "r");
if (fp == NULL)
error("fopen %s failed." file);
while (-1 != fgets (&buf, fp))
{
if (root == NULL)
{
root = @list_type;
tail = root;
}
else
{
tail.next = @list_type;
tail = tail.next;
}
tail.text = buf;
tail.next = NULL;
}
() = fclose (fp);
return root;
}
fopen, fclose, fputs, error