More binary I/O problems

Charles Hixson via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Fri Apr 8 13:58:06 PDT 2016


>
> On 03/25/2016 11:32 AM, Adam D. Ruppe via Digitalmars-d-learn wrote:
>> On Friday, 25 March 2016 at 18:25:28 UTC, Charles Hixson wrote:
>>> But when I try to cast a Chnk to a ubyte[], I get an error, and
>>
>> rawWrite takes a generic array of anything... you should be able to 
>> rawWrite((&your_object)[0 .. 1])
>>
> Thanks, that compiled.  It's running a test now...and I'm leaving it 
> for a few hours.
>
And that worked, but suddenly (after a compiler upgrade, did that 
matter? I'd also changed the program, though in ways that shouldn't have 
affected this.) it stopped working with the message:
let4a.d(138): Error: no [] operator overload for type Chnk

Well, there isn't, Chnk is just a struct.
The offending line was:
f.rawWrite (&(val)[0..1]);
which I already though was unreasonably convoluted.  I just wanted 
something like:
f.write(&val, val.sizeof);
which used to work, but the corresponding read doesn't seem to exist 
anymore, so I can't test it.  It's documented, but when I try to use it:
f.read(&c, c.sizeof);
results in:
let4a.d(172): Error: template std.file.read cannot deduce function from 
argument types !()(File, Chnk*, ulong), candidates are:
/usr/include/dmd/phobos/std/file.d(222):        std.file.read(R)(R name, 
size_t upTo = size_t.max) if (isInputRange!R && 
isSomeChar!(ElementEncodingType!R) && !isConvertibleToString!R)
/usr/include/dmd/phobos/std/file.d(248): std.file.read(R)(auto ref R 
name, size_t upTo = size_t.max) if (isConvertibleToString!R)

And I'm trying to do a binary read, not a string read.  (I also tried it 
with f.read(c, c.sizeof); and got equivalent error messages.

And the struct was (abbreviated as noted):
struct    Chnk
{
   char[20]    wrd;
   ubyte        length;
   ulong        id;

     this    (ulong id,    string wrd)   {  ...   }

     uint    hCode ()    {    return    hash(wrdStr) % fcnt;    }

     string    toString ()    {   ... }

     string    wrdStr()    {  ... }
}    //    struct    Chnk

There are no indirections.  It's just a simple struct that I want to 
read and write to a randomly accessible file.  This used to be easy, but 
now I keep running into problems.

FWIW, it's stored in a ChnkTbl, thus:
const    ushort    fcnt    =    128;
alias    Chnk[string]    ChnkTbl;
ChnkTbl[fcnt]    chnks;

with the idea that each ChnkTbl would be stored in a separate file. But 
it's the binary read/write I keep having problems with.


More information about the Digitalmars-d-learn mailing list