Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

AppGameKit Classic Chat / Possible bug with FileEOF ?

Author
Message
Phaelax
DBPro Master
21
Years of Service
User Offline
Joined: 16th Apr 2003
Location: Metropia
Posted: 4th Nov 2013 06:25 Edited at: 4th Nov 2013 06:26
I have a file which is only 36 bytes (9 integers). So technically this loop should only iterate once. The 9th byte being read is the field you see called "triggers". Then the loop goes back to the beginning of the while loop which should return 1 in the EOF statement since there are no more bytes to read. I verified the source data file in a hex editor to make sure there are no lingering bytes at the end. I don't know why, but this WHILE loop is doing a second iteration and I receive no errors on any of the read commands the second time around.




I attached the triggers.dat file. And below is my output from the above code.


Attachments

Login to view attachments
Marl
12
Years of Service
User Offline
Joined: 19th Nov 2011
Location: Bradford, UK
Posted: 4th Nov 2013 11:34
I had similar issue and solved it by switching to repeat/until.

Logic wise ( for a non-empty source file ) it should do the same, but for me repeat/until worked and while/endwhile didn't.

...and because it solved it for me I didn't bother investigating further.
Van B
Moderator
21
Years of Service
User Offline
Joined: 8th Oct 2002
Location: Sunnyvale
Posted: 4th Nov 2013 11:55
I wonder if the fileEOF command only returns 1 if the file is past the end of the file.

Like...

While fileEOF(1)=0
Read etc
Endwhile

Is running an extra loop, because once you've read the 9 values the file still hasn't passed it's end point - it'll do that the next time it tries to read from it, so when it comes around to check the While condition, it's still false, so goes through the whole thing again.

But if you your a Repeat...Until - the check would be done at the end, it might still have the same problem of course - just pointing out the main difference between the loop types.

The way around it, is to just not do it that way

Like, the code makes a lot of assumptions that get it in trouble when it can't detect the end of the file. I would suggest having control values so you know what exactly is coming next in a file. You might save an extra integer that holds a code for example. Code 1 might be zone, x, y, trigger etc etc - and when you read a 1, you know to read the rest of the values - but have it only read the data that it expects to find... if it finds a null byte at the end of the file, then it wouldn't bother trying to read any more.

Might be an idea, you could save all sorts of different data in different files, have a level file able to designate an items properties, or an enemy character. I tend to work like this but with more abstraction, like a script file doing the job of a level file. It ends up a little bloated, but opens up a whole load of possibilities. Like, in EDS there are character scripts for each character, specifying the stats, starting inventory etc...but it also allows the colouring of specific items, including the colour of the default hands item - so we get skin colouring for free
Just spit-balling.

I am the one who knocks...
Markus
Valued Member
20
Years of Service
User Offline
Joined: 10th Apr 2004
Location: Germany
Posted: 4th Nov 2013 12:04 Edited at: 4th Nov 2013 12:05
...

AGK 108 B19 : Windows 8.1 Pro 64 Bit : AMD Radeon HD 6670
Ancient Lady
Valued Member
20
Years of Service
User Offline
Joined: 17th Mar 2004
Location: Anchorage, Alaska, USA
Posted: 4th Nov 2013 15:18
Phaelax, you are using binary read commands on a text file. That is why the loop is executing more than you expect.

ReadInteger will read four bytes from the file and convert them to integer values.

If you want to use a text, human readable, file, you should change this:
tmp = readInteger(fid)

to this:
tmp = Val(ReadLine(fid))

The ReadInteger/Float/String commands work with binary files created with WriteInteger/Float/String commands.

The only two commands for text, human readable, files are ReadLine and WriteLine.

Cheers,
Ancient Lady
AGK Community Tester and AppGameKit Master
Marl
12
Years of Service
User Offline
Joined: 19th Nov 2011
Location: Bradford, UK
Posted: 4th Nov 2013 15:26 Edited at: 4th Nov 2013 15:31
Quote: "Phaelax, you are using binary read commands on a text file."

If you check, you'll find the file is actually binary, it contains;

36 bytes as stated in the OP

The text shown in the post is the output sample.

So while your advice is useful in general, it is not applicable in this case.
Van B
Moderator
21
Years of Service
User Offline
Joined: 8th Oct 2002
Location: Sunnyvale
Posted: 4th Nov 2013 15:36
I have had some real problems reading strings, like having to truncate non-ascii characters to get filenames to work, that sort of thing. Might even be just a null byte at the end of your file causing problems.

Actually, I think I ended up compiling the string with code, reading the raw data in as bytes and converting to text. Annoying, but at least it gives the chance to skip ascii values that cause problems.

I am the one who knocks...
Phaelax
DBPro Master
21
Years of Service
User Offline
Joined: 16th Apr 2003
Location: Metropia
Posted: 4th Nov 2013 15:55
Quote: "Might even be just a null byte at the end of your file causing problems."

I thought about that, which is why I checked the file in a hex editor. But I'm not even using strings, the file only contains binary integer values.

Quote: "I had similar issue and solved it by switching to repeat/until."

That works for me. Odd.

Quote: " I would suggest having control values so you know what exactly is coming next in a file"

The first integer value is actually the number of entries the file contains, so technically I could just use a FOR loop. (which is probably safer anyway)

Ancient Lady
Valued Member
20
Years of Service
User Offline
Joined: 17th Mar 2004
Location: Anchorage, Alaska, USA
Posted: 4th Nov 2013 16:09
Quote: "If you check, you'll find the file is actually binary, it contains;"

I didn't check the actual file. I was just looking at the posted bits. My bad. Sorry. I had just finished with another post where the issue was using binary reads on a text file. So, that was the 'solution' I thought I saw when I (too) quickly read the OP.

And the use of 'while' should work.

I have not had any problems with FileEOF before.

Just a thought. But, if the file is always only going to contain just the one set of 9 values, there is no need to use the while loop at all. Just read the values in the order expected and close it.

Cheers,
Ancient Lady
AGK Community Tester and AppGameKit Master
Phaelax
DBPro Master
21
Years of Service
User Offline
Joined: 16th Apr 2003
Location: Metropia
Posted: 4th Nov 2013 23:11
Quote: ", if the file is always only going to contain just the one set of 9 values, there is no need to use the while loop at all"


It can contain dozens of entries, each entry being comprised of 8 integers. But the first integer written to the file is the number of entries, so technically I should probably be using a FOR loop anyway.

Ancient Lady
Valued Member
20
Years of Service
User Offline
Joined: 17th Mar 2004
Location: Anchorage, Alaska, USA
Posted: 5th Nov 2013 00:18 Edited at: 5th Nov 2013 03:11
EDIT: Never mind what I said before. I just reread the OP again (and saw the duplicated output).

EDIT 2: I fixed a code error in the third line, it was missing a close parenthesis.

Let's see what size AppGameKit thinks the file is. Try changing your code to this:


Cheers,
Ancient Lady
AGK Community Tester and AppGameKit Master
Phaelax
DBPro Master
21
Years of Service
User Offline
Joined: 16th Apr 2003
Location: Metropia
Posted: 5th Nov 2013 00:21
Quote: "Your output indicates that it is, in fact, not executing a second iteration at all. It skips to the line after 'endwhile' after checking and discovering that the end of file has been reached."

My output does indicate it's going through twice, which it shouldn't be. Why is it going through the second time when there shouldn't be anymore data to read?

Ancient Lady
Valued Member
20
Years of Service
User Offline
Joined: 17th Mar 2004
Location: Anchorage, Alaska, USA
Posted: 5th Nov 2013 00:24
You read too fast. I just edited that post because I spotted my own error.

Cheers,
Ancient Lady
AGK Community Tester and AppGameKit Master
Phaelax
DBPro Master
21
Years of Service
User Offline
Joined: 16th Apr 2003
Location: Metropia
Posted: 5th Nov 2013 03:07

Login to post a reply

Server time is: 2024-04-25 11:33:30
Your offset time is: 2024-04-25 11:33:30