read/peek and automatically advance index in buffer

jwatson-CO-edu real.name at colorado.edu
Thu Mar 16 18:39:00 UTC 2023


I read a file into a `ubyte` buffer as shown:

```d
\\ ...
buffer = cast(ubyte[]) read( fName ); // Read the entire file as 
bytestring
marker = 0; // Current index to read from, managed manually, :(
\\ ...
```

The data file contains numbers of varying size, and I want to 
read them sequentially.
I gleaned the following from forum posts:

```d
// Start read at marker, cast as int
int rtnVal = peek!(int, Endian.bigEndian)(buffer[marker..$]);
marker += 4; // I just peeked 32 bits
```

[The docs 
imply](https://dlang.org/library/std/bitmanip/peek.html) that I 
can peek and also advance my index appropriately, but I'm unsure 
how to specify the return type, endianness, and index pointer all 
at once.  The following does not work:

```d
int rtnVal = buffer.peek(int,Endian.bigEndian)(&marker);
// Error: found `,` when expecting `.` following int
```

What is the idiom / function call that will automatically advance 
my `marker` index variable?


More information about the Digitalmars-d-learn mailing list