readExact Problem
Charma
Motoko_Kusanagi at web.de
Fri May 25 00:54:50 PDT 2007
Daniel Keep Wrote:
>
>
> 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/
well... i know... i am newbie with D T_T sorry...
this is the way i wrote it now and it seams to work perfectly:
File index = new File(indexpath, FileMode.In);
byte AmoundVFS;
index.read(AmoundVFS);
byte t;
char[][] VFSfile;
VFSfile.length = AmoundVFS;
for(int i=0;i<AmoundVFS;i++)
{
index.readExact(&t, 1);
VFSfile[i].length = t;
index.readExact(VFSfile[i].ptr, t);
}
AmoundVFS is saved in the very first byte of the file... i didn't mention it. Why i used uint for t the last time was because i thought that this could be the error since in Phobos it says that it want's a uint as parameter there...
I hope this code is good now. If there is anything to imporve, let me know! thanks for the help!
More information about the Digitalmars-d
mailing list