help-octave
[Top][All Lists]
Advanced

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

Re: End of File test is stopping on empty lines


From: Peter Newman
Subject: Re: End of File test is stopping on empty lines
Date: Mon, 9 Jul 2012 13:51:16 -0400

Thank you Michael!
Both empty and full lines are characters, but the fgetl function returns a double when it reaches the EOF.
Here is the while loop condition that correctly reads empty and full lines till the EOF, while only printing full lines.

1;
fid=fopen("emptyline.txt");
l = fgetl (fid);
while ischar(l)   
    if ~isempty(l)
        printf("%s\n", l);
   endif
   l = fgetl (fid);   
end

On Mon, Jul 9, 2012 at 4:00 AM, Michael Goffioul <address@hidden> wrote:


On Mon, Jul 9, 2012 at 4:01 AM, Peter Newman <address@hidden> wrote:
I  simplified the code to show the essence of the problem.
Here is the code that should skip an empty line...

1;
fid=fopen("emptyline.txt");
l = fgetl (fid);
while (l~=(-1))   
    if ~isempty(l)
            printf("%s\n", l);
   endif
l = fgetl (fid);
end

Here is the file contents of "emptyline.txt"
0010
0020

0030
0040

Why does the while loop exit on an empty line?

See the result of:

'' ~= -1
'abcde' ~= -1

and you'll understand why your loop stops at the first empty line (your loop test does a element-wise vector comparison; with an empty string, the result is an empty matrix ([]) and this is equivalent to false).

Michael.




reply via email to

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