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: Sun, 8 Jul 2012 23:01:02 -0400

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?


On Sat, Jul 7, 2012 at 12:14 PM, Peter Newman <address@hidden> wrote:
I have the following code to read and process lines in a text file that includes a test to skip blank lines. I find that the program completes running after reaching the first empty line rather than skipping and reading the rest of the file, yet the result from fgetl is different when reading empty lines than reading EOF, so I expected this code to work.

1;
fid=fopen("data.txt");
l = fgetl (fid);                 % initialize with first line
lc = l(7:53);                   % take data from center
dblc = deblank (lc);        % strip trailing spaces
cc = strsplit (dblc, ' ');    % separate byte strings
ncc = length (cc);       
hn = ncc/2;
for j = 1:hn;            % join two bytes
    ib(j) = strcat (cc(2*j-1),cc(2*j));
endfor
l = fgetl (fid);
% Loop through data file until we get a -1 indicating EOF
while (l~=(-1))   
    if ~isempty(l)            % only process non-empty lines
       lc = l(7:53);            % take data from center
       dblc = deblank (lc);        % strip trailing spaces
       cc = strsplit (dblc, ' ');    % separate byte strings
       ncc = length (cc);       
       hn = ncc/2;
       clear cv            % clear left overs from last line
       clear bs
       for j = 1:hn;            % join two bytes
          bs(j) = strcat (cc(2*j-1),cc(2*j));
       endfor
           ib = [ib, bs];            % append data from this line
   endif
   l = fgetl (fid);            % get another line   
end

Here is the contents of data.txt where line 1 starts with 0010:

0010  05 dc 63 f4 40 00 40 06 4f af c0 a8 00 1e c0 a8   address@hidden@.O.......
0020  00 0a 0e a4 ee 49 47 a8 ab ee 71 c2 12 55 80 10   .....IG...q..U..
0030  0b 68 a9 1b 00 00 01 01 08 0a ff ff c8 fa ff ff   .h..............
0040  cc 41 44 02 00 00 01 00 40 02 52 02               address@hidden

0050  4e 02 50 02 4e 02 49 02 49 02 45 02 44 02 44 02   N.P.N.I.I.E.D.D.
0060  44 02 43 02 43 02 44 02 43 02 40 02 47 02 45 02   address@hidden
0070  45 02 4f 02 52 02 52 02 52 02 47 02 42 02 41 02   E.O.R.R.R.G.B.A.
0080  42 02 42 02 42 02 42 02 42 02 44 02 40 02         address@hidden

Any suggestions would be appreciated.



reply via email to

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