readExact Problem

Daniel Keep daniel.keep.lists at gmail.com
Fri May 25 00:42:50 PDT 2007



Charma wrote:
> hello,
> I have following problem i can't solve for a whole day:
> I got a file in which there are alternating 1 byte in which a name-lengh is saved, then the name, then another byte with a name length and so on...
> Now i got this piece of code:
> 
> scope uint t;

The "scope" there isn't going to do anything.

> scope char VFSfile[][];

Or there.

> scope char[] temp;

Or there.  "scope" only changes behaviour for class instances, nothing
else.  Although believe me, I wish it *did* work for arrays... man,
that'd be sweet.

> for(int i=0;i<AmoundVFS;i++)

I *could* say you should be using a size_t, or at least an unsigned
type, but that would just be me being pedantic.  Plus, I don't know what
type AmoundVFS is, and how this is being used, so I'll keep quiet :P

> {
> 	index.readExact(&t, 1);

Didn't you say the name length was one byte?  So why are you reading
into a four-byte number?

  ubyte t;
  index.read(t);

> 	writefln(t);
> 	index.readExact(VFSfile.ptr, t); // <-- error here "array bounds something

Whoa, whoa, whoa.  Two things.  First of all, VFSfile.ptr is of type
char[]*: a pointer to an array of characters.  Secondly, you haven't
allocated any space for VFSfile yet, so there's nothing for readExact to
read into.

  VFSfile[i].length = t;
  index.readExact(VFSfile[i].ptr, t);

And you need this outside the loop somewhere:

  VFSfile.length = AmoundVFS;

> 	index.readExact(VFSfile.ptr, t); // this way cauzes Error: not enough data in stream, eventhough i have checked that t is correctly read and the file is big enough

Why are you reading it twice?

> }

That's all the bugs I could see with it.  Hope this helps.

    -- Daniel

-- 
int getRandomNumber()
{
    return 4; // chosen by fair dice roll.
              // guaranteed to be random.
}

http://xkcd.com/

v2sw5+8Yhw5ln4+5pr6OFPma8u6+7Lw4Tm6+7l6+7D
i28a2Xs3MSr2e4/6+7t4TNSMb6HTOp5en5g6RAHCP  http://hackerkey.com/



More information about the Digitalmars-d mailing list