What limit have I run into?

May 20, 2008
10,679
83
Syracuse, NY, USA
This BTM will copy a file.

Code:
setlocal
set nReadSize=2045
set nReads=%@eval[ceil(%@filesize[%1]/%nReadSize)]
set hIn=%@fileopen[%1,r,b]
set hOut=%@fileopen[%2,w,b]
on break goto close
do i=1 to %nReads
    set junk=%@filewriteb[%hOut,-1,%@filereadb[%hIn,%nReadSize,x]]
enddo
:close
set junk=%@fileclose[%hOut]
set junk=%@fileclose[%hIn]

If I change nReadSize to 2046 (and use a file bigger than that) it fails as follows.

The DO loop never terminates. And, each time through the DO loop what's written to the output file is: the first 2046 bytes of the input file followed by 0x40 0xFF.

What's happening? [It's a counted DO loop so I can't understand how it could fail to terminate regardless of whether the file read/writes are correct.]
 
May 20, 2008
10,679
83
Syracuse, NY, USA
The do loop would terminate correctly but it seemed to go on forever because @FILEWRITEB went haywire. Maybe this will shed some light on what's happening. Here is a single manual read/write of 2045 bytes; all seems well. The DATA variable has the form "0xHH 0xHH ..." so the lengths are correct.

Code:
v:\> echo %@repeat[x,2050] > in.txt

v:\> set hin=%@fileopen[in.txt,r,b]

v:\> set hout=%@fileopen[out.txt,w,b]

v:\> set data=%@filereadb[%hin,2045,x]

v:\> echo %@len[%data]
10224

v:\> echo %@filewriteb[%hout,-1,%data]
2045

v:\> echo %@fileclose[%hin] %@fileclose[%hout]
0 0

v:\> dir /k /m in.txt;out.txt
2020-10-15  15:18           2,052  in.txt
2020-10-15  15:20           2,045  out.txt

Here's the same for 2046 bytes. It takes a while because the one @FILEWRITEB actually writes 67,580 bytes!

Code:
v:\> set hin=%@fileopen[in.txt,r,b]

v:\> set hout=%@fileopen[out.txt,w,b]

v:\> set data=%@filereadb[%hin,2046,x]

v:\> echo %@len[%data]
10229

v:\> echo %@filewriteb[%hout,-1,%data]
67580

v:\> echo %@fileclose[%hin] %@fileclose[%hout]
0 0

v:\> dir /k /m in.txt;out.txt
2020-10-15  15:15           2,052  in.txt
2020-10-15  15:16          67,580  out.txt
 
Aug 9, 2009
174
0
It all works fine if you specify "1" data=%@filereadb[%hin,1,x] or am i missing something