help-octave
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

fgetl.m


From: Wonkoo Kim
Subject: fgetl.m
Date: Wed, 06 Sep 95 20:04:25 -0400

> Date: Wed, 06 Sep 95 15:53:16 -0500
> From: "John Eaton" <address@hidden>
> Cc: address@hidden
> Subject: Re: Reading to end-of-file (fgetl.m)
> 
> I didn't see the file fgetl.m, 

Sorry, I posted to the wrong (old?) list (address@hidden) and 
hence it was not posted.  I didn't realize that the octave list addr was 
changed, or did I have a wrong addr?  (Did I miss some announcement?)
Anyway, I attached my version of fgetl.m below (bug fixed).

> but the functions fgetl and fgets will
> both be available as built-in functions in the next release.  Then you
> can write something like
> 
>   while (isstr (s = fgetl ("file")))
>     printf ("%s\n", s);
>   end
> 
> to read all the lines from a file.  No error message will be printed
> when the end of the file is reached.
> 
> You will also be able to limit the number of characters that are read
> by adding a second parameter to fgetl/fgets.  For example,
> 
>   while (isstr (s = fgetl ("file", 10)))
>     printf ("%s\n", s);
>   end
> 
> reads the password file in chunks of at most 10 characters.
>
> And no, I don't have a date set for 1.2, but I'm working on it.  Also,
> I don't plan to make a 1.1.2 release because it would take too much
> time away from working on 1.2.

Thanks, I hope to see v1.2 (for OS/2) soon. :-)

------------------- CUT HERE -------------------
function [linestr] = fgetl (infile)

% Octave 1.1.x doesn't have fgetl().  So this is added...
%
% Syntax:   [linestr] = fgetl (infile)
%
% Return:   Read a line of strings until a new line code from infile.
%           Returns -1 if end-of-file occured.
%
% Wonkoo Kim (address@hidden), May 2, 1995

n = 0;
linestr = [];
while (1)
    [ch, count] = fread (infile, 1, 'uchar');
    if count == 0
        n = -1;
        break;
    endif
    if (ch == 13)       % '\r'
        continue;
    endif
    if (ch == 10)       % '\n'
        break;
    endif
    n++;
    linestr(n) = ch;
endwhile
if n < 0
    linestr = n;
else
    linestr = setstr(linestr);
endif

endfunction
------------------- CUT HERE -------------------

//--------------------------------------------------------------------
// Wonkoo Kim
// address@hidden


reply via email to

[Prev in Thread] Current Thread [Next in Thread]