function line = fgets(fid)
%FGETS	Return the next line of the file as a string.
%       FGETS(FID) returns the next line of a file associated with file
%       identifier fid as a MATLAB string. The newline IS included.
%       Use FGETL() to get the next line WITHOUT the newline included.
%	If just an end-of-file is encountered then -1 is returned.
%
%       IMPORTANT: Please note that this is intended to be used ONLY with 
%                  text files. If by mistake you read a "binary" file
%                  without newline characters this routine may take a
%		   long time for large files.
%

%	Jan 92 - 30
%	Feb 92 - 3
%	May 92 - 5
%       Jul 92 - 14
%	Oct 92 - 23, 26
%	Martin Knapp-Cordes, Steve Bangert
%	Copyright (c) 1984-93 by The MathWorks, Inc.
%
%	Algorithm suggested by Cleve Moler.
%	This works for UNIX and PC. UNIX lines end with a Newline and
%	PC files CAN have lines which end with a Carriage Return, Newline.
%	So for those PC files both characters will be returned.
%       Newline = 10 (decimal)
%	Blocksize = 128 (decimal)
%	The ASCII character set is assumed.
%	C "binary" stream files only for fseek to work properly.
%
%----------------------------------------------------------------------------
%
	if (nargin ~= 1)
	    error ('Wrong number of arguments.')
	end

        NEWLINE = 10;
	BLOCKSIZE = 128;

	line = '';
	fidvec = fopen('all');
	for i = 1:size(fidvec,2)
	    if (fid == fidvec(i))
		[block, count] = fread(fid, BLOCKSIZE, 'char');
		if ~count, line = -1; return, end
		index = find(block == NEWLINE);
		if size(index, 1) ~= 0
		    line = setstr(block(1:index(1))');
		    fseek (fid, index(1) - count, 'cof');
		    return
		elseif count < BLOCKSIZE
		    line = setstr(block');
		    return
		end
		while (size(index, 1) == 0 & count == BLOCKSIZE)
		    line = [line setstr(block')];
		    [block, count] = fread(fid, BLOCKSIZE, 'char');
		    index = find(block == NEWLINE);
		end
		if size(index, 1) ~= 0
		    line = [line setstr(block(1:index(1))')];
		    fseek (fid, index(1) - BLOCKSIZE, 'cof');
		    return
		elseif count < BLOCKSIZE
		    line = [line setstr(block')];
		    return
		end
	    end
	end
	error ('Invalid file identifier.')
