BinaryReader returning very large doubles. What type of value should I be returning?

35 Views Asked by At

I'm trying to read a binaryfile. I don't know the structure, but I do have some code written in R that can read it. I'm not familiar with R but have made some progress converting it to C# and struggle at the last bit.

I'm at a point where I need to list out results which I would expect to be a series of float or double.

The R code looks like this (I've removed some of the logic to keep it short):

Rcpp::NumericVector GetSwmmResult(int iType, int iIndex, int vIndex)
{
  int offset;
  std::vector<float> resultvec(SWMM_Nperiods);
  size_t size;
  
  // --- compute offset into output file

  for ( int i=1; i<=SWMM_Nperiods; ++i)
  {
    offset = StartPos + (i-1)*BytesPerPeriod + 2*RECORDSIZE;
    if ( iType == SUBCATCH )
    {
      offset += RECORDSIZE*(iIndex*SubcatchVars + vIndex);
    }
    else return wrap(resultvec);
    
    // --- re-position the file and read the result
    fseek(Fout, offset, SEEK_SET);
    
    size = fread(&resultvec[i-1], RECORDSIZE, 1, Fout);
  }

  return wrap(resultvec);
}

In C# I expected to do something as follows, where br is my BinaryReader object:

public List<double> GetSwmmResult(int iType, int iIndex, int vIndex)
    {
        int offset;
        List<double> resultvec = new();
        int size;

        // --- compute offset into output file
        Debug.WriteLine("SWMM_Nperiods count = " + SWMM_Nperiods);
        for (int i = 1; i <= SWMM_Nperiods; i++)
        {
            Debug.WriteLine("SWMM_Nperiods " + i);
            offset = StartPos + (i - 1) * BytesPerPeriod + 2 * RECORDSIZE;
            if (iType == SUBCATCH)
            {
                offset += RECORDSIZE * (iIndex * SubcatchVars + vIndex);
            }
            else 
            {
                return resultvec;
            }

            // --- re-position the file and read the result
            br.BaseStream.Position = offset;
            resultvec.Add(br.ReadDouble());
            Debug.WriteLine(resultvec[i - 1]);
        }
        return resultvec;
    }

But my C# just returns a load of very large numbers like:

5.058993159887922E-15
3.10628841909217E-16
5.477524451492502E-17

I'm expecting a series of numbers, but in the 100's or 1000's, not such large numbers. Can anybody see how I should be returning values in my C# code using the R function above as a guide? There's a variable in the R code called SEEK_SET. It's not declared anywhere, so I don't understand how it's being used, but suspect it may be what I'm missing.

0

There are 0 best solutions below