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.

DarkBASIC Professional Discussion / SAVE ARRAY TO DATAFILE problem

Author
Message
gp009
15
Years of Service
User Offline
Joined: 31st Mar 2011
Location:
Posted: 5th May 2011 10:15
after doing alot of searches in the forums, none seems to help my situation.

Problem is when SAVE ARRAY TO DATAFILE is not commented out the app crashes when it gets to dim Player(5) as PlayerInfo...

ill post some code and hope someone can figure this out...

IanM
Retired Moderator
23
Years of Service
User Offline
Joined: 11th Sep 2002
Location: In my moon base
Posted: 5th May 2011 14:33
I seem to be having a few problems with that array plug-in lately. I'll try and get to the bottom of it.

In the meantime, can you make sure you are using the very latest version of the plug-ins.

gp009
15
Years of Service
User Offline
Joined: 31st Mar 2011
Location:
Posted: 5th May 2011 15:13
Quote: "In the meantime, can you make sure you are using the very latest version of the plug-ins."


matrix1util 20110420?

yes, I believe I am using the latest
Grog Grueslayer
Valued Member
21
Years of Service
User Offline
Joined: 30th May 2005
Playing: Green Hell
Posted: 5th May 2011 18:29
That code didn't work at first for me until I created a "PlayerSaves" directory then it worked fine (see attached image). Part of the problem may be you're using an absolute path "C:\Space". Try taking that off and using the directory your project .exe and code are in instead and create all needed directories like "PlayerSaves" within your project directory.

gp009
15
Years of Service
User Offline
Joined: 31st Mar 2011
Location:
Posted: 5th May 2011 19:03
I did what you suggested but still crashes.

i find it strange how it seems to work for you, can you post the changes you made please?
Grog Grueslayer
Valued Member
21
Years of Service
User Offline
Joined: 30th May 2005
Playing: Green Hell
Posted: 5th May 2011 19:38
The only thing I did was rem off the SET DIR command.



Attached is a zipped up version of it with the proper directory within.

gp009
15
Years of Service
User Offline
Joined: 31st Mar 2011
Location:
Posted: 5th May 2011 19:51
even the zipped version crashed.

either theres an issue with the lastest array plugin or something on my comp is corrupt ( i really hope not )
IanM
Retired Moderator
23
Years of Service
User Offline
Joined: 11th Sep 2002
Location: In my moon base
Posted: 5th May 2011 20:53 Edited at: 5th May 2011 21:01
... or maybe you haven't got the VC++ runtime installed? If you haven't, then take a look in my thread and download the runtime for your machine from there (instructions available from the same place).

[edit]
BTW, I'd advise you to use the function format of the OPEN DATAFILE TO WRITE command - that allows you to check that the file did indeed open.



gp009
15
Years of Service
User Offline
Joined: 31st Mar 2011
Location:
Posted: 5th May 2011 21:53
ill try it but

my problem is when it gets to dim Player(5) as PlayerInfo thats where the app crashes it WONT even make it to the function in fact if I use any of the array plug-in commands it crashes at the dim Player(5) as PlayerInfo statement.


Quote: "... or maybe you haven't got the VC++ runtime installed? If you haven't, then take a look in my thread and download the runtime for your machine from there (instructions available from the same place)."


yes I do have the VC++ runtime files.
Neuro Fuzzy
19
Years of Service
User Offline
Joined: 11th Jun 2007
Location:
Posted: 5th May 2011 21:57
Try saying:
"C:\\Space\\PlayerSaves\\Player.sav"

in many languages "\" represents an escape character. For example, "\n" is a newline character (so this is ONE character, not two), and if you literally want a backslash, type "\\".

Or you could just use one forward slash, "/"

I've had problems in dbpro because of that.


Tell me if there's a broken link to images in a thread I post, and I'll fix 'em.
gp009
15
Years of Service
User Offline
Joined: 31st Mar 2011
Location:
Posted: 6th May 2011 00:04
searching thru the forums abit more I believe I discovered that I wasnt running the latest version of DBPro ( dont know what earlier version I had ) so I upgrades to version 7.6 and then the code while nolonger crashes at dim Player(5) as PlayerInfo it instead crashes at SAVE ARRAY TO DATAFILE Player(1),1 but there is data written in the the file. the file is created right where its supposed to be now, it just now crashes right after writting data of that array just before closing the file. So now I just have to figure out why it crashes at that point.
Neuro Fuzzy
19
Years of Service
User Offline
Joined: 11th Jun 2007
Location:
Posted: 6th May 2011 01:30
it'd still be a good idea to try what I said. Perhaps the command that closes the file uses escape characters, but the others don't. (it doesn't sound like that's the problem but it never hurts to rule something out.)


Tell me if there's a broken link to images in a thread I post, and I'll fix 'em.
DVader
22
Years of Service
User Offline
Joined: 28th Jan 2004
Location:
Posted: 6th May 2011 05:51 Edited at: 6th May 2011 05:51
I am not a fan of functions in the main, due to this sort of issue. Is it because you have dimmed player(5) inside InitializeVariables() without declaring it as global? I am guessing this could be the root of the problem. Player(5) will not exist as far as Savefile() is concerned. It may not be, as I said I don't often use functions, but looking at it I can't see how that dim statement will work as is.

http://s6.bitefight.org/c.php?uid=103081
Neuro Fuzzy
19
Years of Service
User Offline
Joined: 11th Jun 2007
Location:
Posted: 6th May 2011 06:07
I do believe arrays are created globally. DBPro scoping is weird.


Tell me if there's a broken link to images in a thread I post, and I'll fix 'em.
gp009
15
Years of Service
User Offline
Joined: 31st Mar 2011
Location:
Posted: 6th May 2011 06:32 Edited at: 6th May 2011 06:39
I rewritten some code twice like this:



at this point I'm doubting the code is not causing the crash. it could be the array plugin-IanM said he was looking at the plugin.

in meantime, I messed with the windows UAC settings, read & write permissions,ect for the projects executable and none of those helped.


make directory "PlayerSaves" was added to make sure the directory path exists--if it already exists--its a silent fail but doesn't crash the app--it just moves on to next statement.

so basically at this point it just crashes just after the data is written(or while data is being written) to file but before the close datafile 1 statement

[edit]

btw the Player.sav file is only created and written when I run it in debug-step by step mode but does not when run at full speed which is pretty wierd. I'll mess with the code some more in meantime.
gp009
15
Years of Service
User Offline
Joined: 31st Mar 2011
Location:
Posted: 6th May 2011 08:17 Edited at: 6th May 2011 08:30
ok, I just updated DBPro to latest which is 7.7 and now everything is working as it should. no more crashes

as it all turned out to be my root of the problems were:

1. old version of DBPro ( 3.7 or earlier )
2. windows SP1 which killed all the executeables after updating to DBPro 7.6
3. read & write permissions ( curse that UAC )

so I basically updated DBPro to latest and changed the read and write permissions and the crash stopped.


Thanks for the help guys. Its nice to see that dark basic is still alive and kicking.
WLGfx
18
Years of Service
User Offline
Joined: 1st Nov 2007
Location: NW United Kingdom
Posted: 6th May 2011 13:33 Edited at: 6th May 2011 13:33

Only defines your array to hold one element. So:


Is saving the incorrect element I presume. Should it not be:



Warning! May contain Nuts!
IanM
Retired Moderator
23
Years of Service
User Offline
Joined: 11th Sep 2002
Location: In my moon base
Posted: 6th May 2011 15:22
The first line you highlight has allocated 2 items in the array - 0 and 1.

The second line you highlight, the (1) has no effect. As the command/function is expecting an array and not an integer, it gets the array. Using Player() will give the same effect as Player(1).

gp009
15
Years of Service
User Offline
Joined: 31st Mar 2011
Location:
Posted: 7th May 2011 16:30 Edited at: 7th May 2011 16:39
interesting... thanks IanM ill keep that in mind.



[edit]

I just thought the number in the () would be refering to a record number in a file kinda like random access in some other basic languages would.

for example:


player record 1
players name
high score

player record 2
players name
high score

I havent really tried having data stored in that way yet, but would be great if it can as im about to start a small data file for items information instead of creating a file for each item which would result 100s of files.
Grog Grueslayer
Valued Member
21
Years of Service
User Offline
Joined: 30th May 2005
Playing: Green Hell
Posted: 7th May 2011 18:26
Quote: "I just thought the number in the () would be refering to a record number in a file kinda like random access in some other basic languages would."


It saves the entire array no matter what number is used (or lack of a number as IanM said). I tend to use zero for any command that requires an array name like "Player(0)".

Quote: "instead of creating a file for each item which would result 100s of files."


Yeah you don't want or need to create separate files for each item. The beauty of UDTs is that they allow us to mix data types in one array so everything we need for that particular thing is in the same spot.

IanM
Retired Moderator
23
Years of Service
User Offline
Joined: 11th Sep 2002
Location: In my moon base
Posted: 8th May 2011 00:23
@gp009,
If you want to save/load a single array element, then you currently have two choices.

1. Define another array of the same type but with only an element 0. Then when you want to save, copy your item to this array, then save it. Loading is the reverse.

2. Write functions that will save/load the items field-by-field.

I'll have a think about automating things, but there are a couple of issues I'd need to think about.

gp009
15
Years of Service
User Offline
Joined: 31st Mar 2011
Location:
Posted: 10th May 2011 16:02
Quote: "@gp009,
If you want to save/load a single array element, then you currently have two choices.

1. Define another array of the same type but with only an element 0. Then when you want to save, copy your item to this array, then save it. Loading is the reverse.

2. Write functions that will save/load the items field-by-field.

I'll have a think about automating things, but there are a couple of issues I'd need to think about.
"


aye, if only there was a way to do random accsess files dealing with UTD arrays it would REALLY simplify things as you wouldn't have to read the whole file just to get to record 90 but just go directly to that record number instead.

but until dark basic has the ability to do so, I think ill just have to write functions.

[edit] btw has anyone heard of any plugin that would create random accsess files and such?
Grog Grueslayer
Valued Member
21
Years of Service
User Offline
Joined: 30th May 2005
Playing: Green Hell
Posted: 10th May 2011 17:47
Quote: "btw has anyone heard of any plugin that would create random accsess files and such?"


IanMs DATAFILE commands do just that already. You use OPEN DATAFILE TO UPDATE which allows loading/saving at the same time and you use SET DATAFILE POSITION with something like RecordNumber*64 (however many bytes are in each record) to go to the specific spot in the file to load/save that particular record.

Here's the DATAFILE commands:
http://www.matrix1.demon.co.uk/Matrix1Utils_Help/Matrix1Util_22_index.html

The automatic process that IanM is thinking about would make those commands a lot easier to work with... especially with strings because those have to be cut-off or added to (the manual way is with his PADRIGHT$ command) to get the record size just right for the file.

I love the DATAFILE commands and wait with excitement for what IanM comes up with for automation. But if he decides not to make it easier we still have the ability to do it with a little bit of work.

gp009
15
Years of Service
User Offline
Joined: 31st Mar 2011
Location:
Posted: 11th May 2011 08:40
yea,I hope he does find a way to automate the processes, finding the exact number in bytes is alittle tough
IanM
Retired Moderator
23
Years of Service
User Offline
Joined: 11th Sep 2002
Location: In my moon base
Posted: 11th May 2011 15:21
It's not that difficult really - just paying attention to a few small details. I'll post an example when I get home after work.

I am looking into making it into a single-step command, and a relative-record file would be a simple way to make the data retrievable fast - I just need to think of a way to either specify fixed-size strings, or allow the strings to be separately stored and retrievable.

Grog Grueslayer
Valued Member
21
Years of Service
User Offline
Joined: 30th May 2005
Playing: Green Hell
Posted: 11th May 2011 20:18
@ gp009:

You just have to calculate the bytes based on the datatypes you use and determine the max number of characters you want each string to use.





@ IanM:

It would be nice if it was as easy as Quickbasic but it probably can't be done that easy in Darkbasic. In Quickbasic you define the amount of characters you want in the UDT and use GET/PUT to read/write the data to the file. It just automatically calculates where to read/write the file based on the UDT.



Of course whatever you come up with will be cool.

IanM
Retired Moderator
23
Years of Service
User Offline
Joined: 11th Sep 2002
Location: In my moon base
Posted: 11th May 2011 21:55
Here's a very simple example - it generates a relative record file, then reads it back.


One thing to note is that I have NOT padded the string to 30 characters, because in this case it's not necessary - it's only necessary when you wish to read a particular field from within a record, without reading the whole record.

Grog Grueslayer
Valued Member
21
Years of Service
User Offline
Joined: 30th May 2005
Playing: Green Hell
Posted: 12th May 2011 04:41
Quote: "Here's a very simple example - it generates a relative record file, then reads it back."


That's basically how I do it too. It is cool how you determine how many records are in the file... I'm definitely adding that to my current project.

Quote: "One thing to note is that I have NOT padded the string to 30 characters, because in this case it's not necessary - it's only necessary when you wish to read a particular field from within a record, without reading the whole record."


That's interesting. I always thought it was best to pad the strings to make sure the data lined up exactly. Without it though each record is still starting in the right position within the file and saving a bit of space from the last record because it's not padding the strings.

In my current project I'm loading all the data into arrays but I decided about an hour ago to do a major overhaul and only load the data as needed when viewing/editing the data. I won't be padding the strings anymore, thanks IanM.

Login to post a reply

Server time is: 2026-07-11 04:24:34
Your offset time is: 2026-07-11 04:24:34