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

  1. function line = fgetl(fid)
  2. %FGETL    Return the next line of the file as a string.
  3. %       FGETL(FID) returns the next line of a file associated with file
  4. %       identifier fid as a MATLAB string. The newline and a carriage return
  5. %    if it preceeds it are NOT included. Use FGETS() to get the next line
  6. %    with those characters INCLUDED. If just an end-of-file is encountered
  7. %    then -1 is returned.
  8. %
  9. %       IMPORTANT: Please note that this is intended to be used ONLY with 
  10. %                  text files. If by mistake you read a "binary" file
  11. %                  without newline characters this routine may take a
  12. %           long time for large files.
  13. %
  14. %    Example: (equivalent to 'type fgetl.m')
  15. %    
  16. %        fid=fopen('fgetl.m');
  17. %        while 1
  18. %            line = fgetl(fid);
  19. %        if ~isstr(line), break, end
  20. %        disp(line)
  21. %        end
  22. %        fclose(fid);
  23. %
  24.  
  25. %    Jan 92 - 30
  26. %    Feb 92 - 3
  27. %    May 92 - 5
  28. %    Oct 92 - 23
  29. %    Nov 92 - 13, 15, 19
  30. %    Martin Knapp-Cordes, Steve Bangert
  31. %    Copyright (c) 1984-93 by The MathWorks, Inc.
  32. %
  33. %    Algorithm suggested by Cleve Moler.
  34. %    Newline = 10 (decimal)
  35. %    Carriage Return = 13 (decimal)
  36. %    Blocksize = 128 (decimal)
  37. %    The ASCII character set is assumed.
  38. %    C "binary" stream files only for fseek to work properly.
  39. %
  40. %----------------------------------------------------------------------------
  41. %
  42.     if (nargin ~= 1)
  43.         error ('Wrong number of arguments.')
  44.     end
  45.  
  46.     NEWLINE = 10;
  47.     CR = 13;
  48.     BLOCKSIZE = 128;
  49.  
  50.     line = '';
  51.     fidvec = fopen('all');
  52.     for i = 1:size(fidvec,2)
  53.         if (fid == fidvec(i))
  54.         [block, count] = fread(fid, BLOCKSIZE, 'char');
  55.         if ~count, line = -1; return, end
  56.         index = find(block == NEWLINE);
  57.         if size(index, 1) ~= 0
  58.             if index(1) ~= 1 
  59.             if block(index(1) - 1) == CR
  60.                     line = setstr(block(1:(index(1) - 2))');
  61.             else
  62.                     line = setstr(block(1:(index(1) - 1))');
  63.             end
  64.             else
  65.                 line = setstr(block(1:(index(1) - 1))');
  66.             end
  67.             fseek (fid, index(1) - count, 'cof');
  68.             return
  69.         elseif count < BLOCKSIZE
  70.             line = setstr(block');
  71.             return
  72.         end
  73.         while (size(index, 1) == 0 & count == BLOCKSIZE)
  74.             line = [line setstr(block')];
  75.             [block, count] = fread(fid, BLOCKSIZE, 'char');
  76.             index = find(block == NEWLINE);
  77.         end
  78.         if size(index, 1) ~= 0
  79.             if index(1) ~= 1 
  80.             if block(index(1) - 1) == CR
  81.                     line = [line setstr(block(1:(index(1) - 2))')];
  82.                 else
  83.                     line = [line setstr(block(1:(index(1) - 1))')];
  84.             end
  85.             else
  86.                 line = [line setstr(block(1:(index(1) - 1))')];
  87.             end
  88.             fseek (fid, index(1) - BLOCKSIZE, 'cof');
  89.             return
  90.         elseif count < BLOCKSIZE
  91.             line = [line setstr(block')];
  92.             return
  93.         end
  94.         end
  95.     end
  96.     error ('Invalid file identifier.')
  97.