www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - readExact Problem

reply Charma <Motoko_Kusanagi web.de> writes:
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;
scope char VFSfile[][];
scope char[] temp;
for(int i=0;i<AmoundVFS;i++)
{
	index.readExact(&t, 1);
	writefln(t);
	index.readExact(VFSfile.ptr, t); // <-- error here "array bounds something
	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
}

AmoundVFS is the amound of entries, index is the File which i loaded with
"File"...
now this aborts with the very first entry. what i want is, that the VFSfile has
all the names in an array in the end. in a way like this:
VFSfile[0] => "name1",
VFSfile[1] => "name2",
and so on...

anyone can help?!?
May 25 2007
next sibling parent reply Daniel Keep <daniel.keep.lists gmail.com> writes:
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/
May 25 2007
parent reply Charma <Motoko_Kusanagi web.de> writes:
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!
May 25 2007
parent reply Daniel Keep <daniel.keep.lists gmail.com> writes:
Charma wrote:
 well... i know... i am newbie with D T_T sorry...
That's alright; we all have to start from somewhere.
 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;
You should probably use ubyte's for AmoundVFS and t. I don't imagine you're going to have a negative number of entries, or negative length, so you're restricting yourself to 127 entries/characters maximum. Although, if you're conforming to an existing file format, best stick to that :)
 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!
The only other thing I can see is that index.readExact(&t,1) shouldn't be necessary; you should be able to just use index.read(t), which does basically the same thing, but is a bit cleaner. -- 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/
May 25 2007
parent Charma <Motoko_Kusanagi web.de> writes:
Daniel Keep Wrote:

 
 
 Charma wrote:
 well... i know... i am newbie with D T_T sorry...
That's alright; we all have to start from somewhere.
 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;
You should probably use ubyte's for AmoundVFS and t. I don't imagine you're going to have a negative number of entries, or negative length, so you're restricting yourself to 127 entries/characters maximum. Although, if you're conforming to an existing file format, best stick to that :)
 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!
The only other thing I can see is that index.readExact(&t,1) shouldn't be necessary; you should be able to just use index.read(t), which does basically the same thing, but is a bit cleaner. -- 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/
alright, fixed that too ^^;; thanks for all!
May 25 2007
prev sibling parent "David B. Held" <dheld codelogicconsulting.com> writes:
Charma wrote:
 [...]
 anyone can help?!?
One thing that might help a lot is if you take advantage of the built-in unittest feature. So as you are writing your code, when you are not entirely confident that you know how something works, just throw in a unittest { // write short test here } block and build with -unittest to see what D thinks of your code. If you really want to get fancy, you should try Test-Driven Development, which requires that you write your tests first, and then work on your code until they pass. Dave
May 25 2007