gustaf - 2014-08-20

A file bin is opened in native machine binary format. It consists of repetitions of equal-length records. Logically they represent a label of integers and a label-values-dependent bunch of (real) data-records.

A while-loop iterates over the label+data records. This works perfectly until the end of the file is reached: then an error occurs. The reason is that the condition in the while loop does not work; eof(f) is false although all data has been read. That conclusion is based on the successful that precedes. Asymptote says:

eof.asy: 15.28: reading array of length 0 with out-of-bounds index 0

The value of eof(bin) has been output and it is false all the time (as well as error(bin)). The Asymptote script is a translation of a Delphi program that reads the same file and it writes the same values for the label-record data.

Any suggestion of how to get out of this?

Here is the Asymptote code followed by the output.

   int i  ;  // counter
   int Np ;  // number of point,normal pairs in one tristrip
   int Ns ;  // number of tri-strips in this run
   int  []   i6  = new int [6];  // Asymptote(int )=Delphi(LongInt)

   file      bin = input("../dat/tri.bin",mode="binary");

   bin.singlereal();             //  OpenGL(GLFloat) = Asymptote(singlereal)

 // iterate: control record i6, data xij6
 i=0;
 write("run    Nps      Ns");
 while (!eof(bin)){ // DO BEGIN
    i=i+1;
    i6=bin.dimension(6); Np=i6[0]; Ns=i6[2]; // read the "header" of 6 LongInt
    real[][][] xij6=new real[Ns][Np][6];
    xij6=bin.dimension(Ns,Np,6);             // read the data
    write(i,i6[0],i6[2]-6);                  // log the "header"
 }                  // END WHILE


run    Nps      Ns
1       30      265
2       30      98
3       30      102
4       30      8
5       30      92
6       30      8
7       30      91
8       30      8
9       30      91
10      30      8
11      30      91
12      30      8
eof.asy: 15.28: reading array of length 0 with out-of-bounds index 0