forums

TNT Basic Forums > Programming Q&A
File reading/writing problem
< Last Thread     Next Thread >
Author
Thread        Post A Reply

06-22-2007 21:17

Posted by:
swagIT

Location:
Great Wet North (Vancouver)

Find more posts by swagIT

here is what was submitted to me for abug report by a user:


*** Runtime exception caught ***
Line# : 11719
Code : impimgmod=file read int (game1data)
Error : Unexpected end of file.

The first time was early on in the game, the second time was when I was on
lvl 35. I believe this is the June 10, 2007 release.


This happens infrequently - but has occurred to me too.

My save/load willl work 95% of the time but every once and a while I get an
Error : Unexpected end of file.

Anyone have any ideas?

I even tried deleting the file then re-writing it during the save procedure.
I added a delay after closing the file.
Not too sure where to go from here.....

cheers

barry

06-22-2007 22:55

Posted by:
someone

Location:
Quebec ( Canada )

Click Here to Email someone   Find more posts by someone

is the file complete?

do you try reading multiple times from same stream without resetting it back to its intial position or closing/reopening it?

06-22-2007 23:25

Posted by:
swagIT

Location:
Great Wet North (Vancouver)

Find more posts by swagIT

both the load and save procedures close the file at the end
and set the file position to 0 at the beginning.

As I said - the strange thing is that it will work perfectly for hours and then fail for no obvious reason......... that is what is frustrating.

06-23-2007 00:23

Posted by:
allnodcoms

Location:
hertfordshire (England)

Click Here to Email allnodcoms   Find more posts by allnodcoms

A common cause...

Loading an array within a loop that is controlled by a variable outside of the routine (either passed in, global scope or shared). If you load arrays within a loop then that is the first place to look.

Example: You have a ten element array that holds baddies. Your initialisation code sets up these ten baddies and sets a variable to hold the number of live ones. Make sure that the save code and the load code are both using this variable. A common error is the load code trying to load all ten, when only eight were live when it was saved... The load code will load the data, but will read beyond the array and everything else will get out of sync. A good indication is if the error is in a different place each crash (depending on the number of live baddies?).

This is the most common cause of this sort of error Barry, and the first place you should be checking. If this doesn't seem to be the problem then get back to me and I'll see what I can come up with...

Best of luck...

Danny (nod the mod)

06-23-2007 00:33

Posted by:
swagIT

Location:
Great Wet North (Vancouver)

Find more posts by swagIT

By George I think you are on to something there!

---I am hitting my head on my desk as I type this ---

I have a ton of arrays - probably saving over 200 variables each time.

Can I solve this by using a value of "0" to fill the empty parts of the array?




06-23-2007 00:49

Posted by:
someone

Location:
Quebec ( Canada )

Click Here to Email someone   Find more posts by someone

well when you save arrays that have variable lengths usually you should first save array lengths, then contents

06-23-2007 01:02

Posted by:
swagIT

Location:
Great Wet North (Vancouver)

Find more posts by swagIT

Here is an example of how I am saving an array - see anything wrong?

==================== array is defined
global int chests[10]

================== array values are set to 0
for zforer = 1 to 9
chests[zforer]=0
next zforer

================== the utilized parts are assigned a value
for forer =1 to rnder
chestsprite = 60+forer
chests[forer]=chestsprite
next forer

================== when saving file
for saverfor = 1 to 9
File Write game1data, chests[saverfor]
next saverfor

=================== when loading file
for saverfor = 1 to 9
chests[saverfor]=file read int (game1data)
next saverfor

06-23-2007 01:05

Posted by:
swagIT

Location:
Great Wet North (Vancouver)

Find more posts by swagIT

every array I use has a set length and is defined global

06-24-2007 01:56

Posted by:
allnodcoms

Location:
hertfordshire (England)

Click Here to Email allnodcoms   Find more posts by allnodcoms

Consistent save and load procedures...

The Example I gave was just that, an example... The thing you need to be doing is to ensure that the code which saves the file and the code which loads the file use the same logic. Using zeros for unused elements is an option, but not a good one... 'Someone's' point about array lengths is a good one. I've been writing Mac code for about 15 years now, and we always used to use a thing called a PASCAL string (or STR255), it was a maximum of 254 bytes in length, the first byte (byte 0) gave the length of the string, and the bytes from 1 to "contents of byte 0" were the actual data. This is still my preferred method of storage. When using TNT (which uses longs over bytes) you can define the length of a piece of data to be thousands of bytes, this will be more than you could ever need. When you save array data out, first write the number of elements that you save, then write those ints. When you come to re-load that data, load the first int (which is the number of ints saved) and then loop on that value to re-load the rest of the data. This will probably solve your problem at the expense of one long per array (four bytes)...

Have fun...

Danny (nod the mod)

06-24-2007 14:56

Posted by:
someone

Location:
Quebec ( Canada )

Click Here to Email someone   Find more posts by someone

However he says each array has a set length

If this is true, the error should not happen for the reason we pointed out

06-24-2007 15:56

Posted by:
swagIT

Location:
Great Wet North (Vancouver)

Find more posts by swagIT

Yup - all of them are a fixed size - none of the arrays has a variable size - that's what is twisting my poor little brain.

06-24-2007 18:58

Posted by:
allnodcoms

Location:
hertfordshire (England)

Click Here to Email allnodcoms   Find more posts by allnodcoms

An enigma wrapped up in a conundrum then...

Without seeing more of the project I'm at a loss, it still sounds like a logic problem though. If you want to zip it up and mail it over I'll take a look and see what's going on, I kind of need to see it run and see if I can replicate the problem. I'll leave it with you...

Danny (nod the mod)

06-24-2007 19:04

Posted by:
swagIT

Location:
Great Wet North (Vancouver)

Find more posts by swagIT

Thanks Danny - before bothering you with it - I am going to run a battery of tests (picture me spending 11 hours saving, closing, opening, loading, over and over and over...) just to pin-point where the trouble is. If I am completely lost, I may take you up on it - this is something I really SHOULD be able to solve for myself like a big boy....

cheers

barry

06-24-2007 19:12

Posted by:
someone

Location:
Quebec ( Canada )

Click Here to Email someone   Find more posts by someone

When this happens, perhaps keep a copy of the file.

Does it always happen with the same file? That could be a good question to answer. That can pinpoint if it's a problem where the file is not saved correctly, or if it's not laoded correctly.

06-24-2007 19:14

Posted by:
someone

Location:
Quebec ( Canada )

Click Here to Email someone   Find more posts by someone

I see seomthing strange in the code you posted

for saverfor = 1 to 9

shouldn't it be

for saverfor = 0 to 9

? This way you're leaving out first item. Or maybe that's what you want?

06-24-2007 20:05

Posted by:
swagIT

Location:
Great Wet North (Vancouver)

Find more posts by swagIT

Yeah - zeros always cause me problems so If I need an array of 10 I define it as [11] and ignore the first "0" - =)

06-24-2007 20:09

Posted by:
swagIT

Location:
Great Wet North (Vancouver)

Find more posts by swagIT

Yes Someone - basically I have 1 "savedgamefile" that players can load from if they get killed or decide to quit and go back to the game. Once the file is corrupt - it can't be loaded - so to look at it I guess I would have to make a little program in TNT to read what is in the file since it is "data" type not text?

That is a great idea...... now I have to make it not work - so far I haven't been able to replicate the crash after 30 minutes or soo of saving and such.....

Thanks for your help!

06-24-2007 23:43

Posted by:
someone

Location:
Quebec ( Canada )

Click Here to Email someone   Find more posts by someone

reproducability is the key to solving a problem

if you have to wait for hours for it to appear, you might only have gathered a very small amount of information when it happens, and then you need to wait another hour and hope...

usually in such cases i setup some sort of very precise logging of everything that's going on in problematic sections, so when the issues does happen, i can analyse the log and gather needed information.

this may or may not apply to your project, of course.

also, if the code is messy/blocky, cleaning it up can often surprisingly uncover bugs and/or make them easier to find.

All times are GMT        Post A Reply

Forum Jump:
< Last Thread     Next Thread >

< Contact Us - TNT Basic >

Powered by: vBulletin Lite Version 1.0.1 Lite
Copyright © Jelsoft Enterprises Limited 2000.