home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 10 / 10.iso / l / l460 / 2.ddi / IOFUN.DI$ / FGETS.M < prev    next >
Encoding:
Text File  |  1993-03-07  |  2.2 KB  |  71 lines

  1. function line = fgets(fid)
  2. %FGETS    Return the next line of the file as a string.
  3. %       FGETS(FID) returns the next line of a file associated with file
  4. %       identifier fid as a MATLAB string. The newline IS included.
  5. %       Use FGETL() to get the next line WITHOUT the newline included.
  6. %    If just an end-of-file is encountered then -1 is returned.
  7. %
  8. %       IMPORTANT: Please note that this is intended to be used ONLY with 
  9. %                  text files. If by mistake you read a "binary" file
  10. %                  without newline characters this routine may take a
  11. %           long time for large files.
  12. %
  13.  
  14. %    Jan 92 - 30
  15. %    Feb 92 - 3
  16. %    May 92 - 5
  17. %       Jul 92 - 14
  18. %    Oct 92 - 23, 26
  19. %    Martin Knapp-Cordes, Steve Bangert
  20. %    Copyright (c) 1984-93 by The MathWorks, Inc.
  21. %
  22. %    Algorithm suggested by Cleve Moler.
  23. %    This works for UNIX and PC. UNIX lines end with a Newline and
  24. %    PC files CAN have lines which end with a Carriage Return, Newline.
  25. %    So for those PC files both characters will be returned.
  26. %       Newline = 10 (decimal)
  27. %    Blocksize = 128 (decimal)
  28. %    The ASCII character set is assumed.
  29. %    C "binary" stream files only for fseek to work properly.
  30. %
  31. %----------------------------------------------------------------------------
  32. %
  33.     if (nargin ~= 1)
  34.         error ('Wrong number of arguments.')
  35.     end
  36.  
  37.         NEWLINE = 10;
  38.     BLOCKSIZE = 128;
  39.  
  40.     line = '';
  41.     fidvec = fopen('all');
  42.     for i = 1:size(fidvec,2)
  43.         if (fid == fidvec(i))
  44.         [block, count] = fread(fid, BLOCKSIZE, 'char');
  45.         if ~count, line = -1; return, end
  46.         index = find(block == NEWLINE);
  47.         if size(index, 1) ~= 0
  48.             line = setstr(block(1:index(1))');
  49.             fseek (fid, index(1) - count, 'cof');
  50.             return
  51.         elseif count < BLOCKSIZE
  52.             line = setstr(block');
  53.             return
  54.         end
  55.         while (size(index, 1) == 0 & count == BLOCKSIZE)
  56.             line = [line setstr(block')];
  57.             [block, count] = fread(fid, BLOCKSIZE, 'char');
  58.             index = find(block == NEWLINE);
  59.         end
  60.         if size(index, 1) ~= 0
  61.             line = [line setstr(block(1:index(1))')];
  62.             fseek (fid, index(1) - BLOCKSIZE, 'cof');
  63.             return
  64.         elseif count < BLOCKSIZE
  65.             line = [line setstr(block')];
  66.             return
  67.         end
  68.         end
  69.     end
  70.     error ('Invalid file identifier.')
  71.