fread(..inf..skip) returns to many elements

Alois Schloegl alois.schloegl at gmx.net
Fri Feb 29 03:28:36 CST 2008


John,

thanks. the patch solved the problem.

Alois



John W. Eaton wrote:
> On 27-Feb-2008, Alois Schloegl wrote:
>
> | To: bug at octave.org
> | Cc: a.schloegl at ieee.org
> | Subject: bug in fread with skip
> | --------
> | Bug report for Octave 3.0.0+ configured for i686-pc-linux-gnu
> | 
> | Description:
> | -----------
> | 
> | I came across a problem using a rather complex call to fread.
> | The file contains some header (10752 bytes) and 40 data blocks with 
> | 62976 bytes.
> | This results in a file size of 2 555 904 bytes. When, I tried to read 
> | the last 1536 bytes from each data block, using this command,
> | [t,c] = fread(fid,inf,'1536*uint8',61440);
> | 
> | one gets 98304 bytes, this is much more than the expected 40*1536=61440.
> | 
> | 
> | The problem appears in Octave 2.9.19 as well as in 3.0.0+ a recent CVS 
> | snapshot (from Feb 24).
> | 
> | 
> | 
> | 
> | Repeat-By:
> | ---------
> | 
> | Here is a script for repeating the problem,
> | 
> | % generate test file with 2 555 904 bytes
> | testfile='/tmp/test.bin';
> | fid = fopen(testfile,'w');
> | fwrite(fid,1:638976,'uint32');
> | fclose(fid);
> | 
> | % read 1536 bytes and skip 61440 byes as long as data elements are available
> | fid = fopen(testfile,'r');
> | fseek(fid,10752+61440,'bof');
> | [t,count] = fread(fid,inf,'1536*uint8',61440);
> | fclose(fid);
> | count,
> | 
> | length(t) and count have a value of 87552 instead of 61440.
>
> Please try the following patch.
>
> Thanks,
>
> jwe
>
>
>   
> ------------------------------------------------------------------------
>
> # HG changeset patch
> # User John W. Eaton <jwe at octave.org>
> # Date 1204170478 18000
> # Node ID 2c4b0cbda85aa48fa98cde1028c84e4e1ff1ea02
> # Parent  a2950622f070e8ff747fca0a2fe6685cda9fdb11
> oct-stream.cc (do_read): stop reading if seek fails
>
> diff --git a/src/ChangeLog b/src/ChangeLog
> --- a/src/ChangeLog
> +++ b/src/ChangeLog
> @@ -1,3 +1,7 @@ 2008-02-26  John W. Eaton  <jwe at octave.o
> +2008-02-27  John W. Eaton  <jwe at octave.org>
> +
> +	* oct-stream.cc (do_read): Stop reading if seek fails.
> +
>  2008-02-26  John W. Eaton  <jwe at octave.org>
>  
>  	* ov-base-int.cc (octave_base_int_helper,
> diff --git a/src/oct-stream.cc b/src/oct-stream.cc
> --- a/src/oct-stream.cc
> +++ b/src/oct-stream.cc
> @@ -3139,13 +3139,15 @@ do_read (octave_stream& strm, octave_idx
>  		  elts_read++;
>  		}
>  
> +	      int seek_status = 0;
> +
>  	      if (skip != 0 && elts_read == block_size)
>  		{
> -		  strm.seek (skip, SEEK_CUR);
> +		  seek_status = strm.seek (skip, SEEK_CUR);
>  		  elts_read = 0;
>  		}
>  
> -	      if (is.eof ())
> +	      if (is.eof () || seek_status < 0)
>  		{
>  		  if (nr > 0)
>  		    {
>   



More information about the Bug-octave mailing list