getline
getline(fid[, varname])
This function reads the next line from the file associated with the file identifier fid, which must be a return value from a previous call to open.
fid must refer to a file that was opened for reading.
varname must be either a scalar variable or an array element. If varname is given, then the line is placed in the variable by that name. The return value in this case is the number of characters read. If the end of the file is reached before reading any characters, getline returns 0 and sets varname to the null string. If varname is not given, then getline returns the line or a null string if the end of file has been reached.
• getline does not remove the terminating newline character. If desired, use the function chop to do this.
• getline has an internal limit of 3000 characters. If a newline character is not found within the limit, then that many characters are returned without the terminating newline character.
• getline does not support reading lines that contain null characters (\000), that is, binary data, because it is implemented using the C language function fgets. The read function can be used to read binary data.
Examples
The following is an example of a function to copy one file to another, using getline and put:
function fid_copy(from, to)
{
local inf, outf, line
inf = open(from, "r")
if (inf < 0) {
eval "Couldn't open file for read:", from
return 0
}
outf = open(to, "w")
if (outf < 0) {
close(inf)
eval "Couldn't open file for write:", to
return 0
}
while (getline(inf, line)) {
put(outf, line)
}
close(outf)
close(inf)
return 1; # True
}
Related Topics