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.

Newcomers DBPro Corner / Text File Problems

Author
Message
Michael73
14
Years of Service
User Offline
Joined: 29th Jan 2010
Location:
Posted: 11th Sep 2010 11:35
Hello,
Im just having a few problems with reading from a text file to a variable. It should read the number stored into the file and store it to use in the game, but the number it stores in the variable is alot different(like -347695475984) from the one stored in the file. Why is this happening? If it helps, here is the code related to the file reading:
Grog Grueslayer
Valued Member
19
Years of Service
User Offline
Joined: 30th May 2005
Playing: Green Hell
Posted: 11th Sep 2010 12:17 Edited at: 11th Sep 2010 12:30
It stores it as an integer which you can't really read using a text editor. If you want to be able to read it as a .txt file you need to store the score as a string instead. Use STR$() to convert a number to a string and use VAL() to convert a string to a number.



Also it's better to check if the file exists before you open it rather than checking if the file is open after the opening of the file.







And if the above line is at the top of your code there's really no need for it. Any variables that aren't defined by you automatically start at zero when Darkbasic sees them for the first time. Even though there is no "Score=0" in the following code snip it starts out at zero when the variable Score is first used.



Michael73
14
Years of Service
User Offline
Joined: 29th Jan 2010
Location:
Posted: 12th Sep 2010 05:41
Thanks, that helped me lots. But now I'm getting an error saying 'Cannot perform 'integer' cast on type '$$1' at line 23'. and all line 23 is is declaring the 'score' variable(score="120"). Im setting 'score' as a string so if i ever need to use it as a number i can just use VAL.
LBFN
17
Years of Service
User Offline
Joined: 7th Apr 2007
Location: USA
Posted: 12th Sep 2010 15:28
Use
to use it as a string.

You could also use SCORE$. You could also leave it as an integer (typical for me) and use the TEXT command along with STR$ to print out it's value.


So many games to code.......so little time.
Mychal B
14
Years of Service
User Offline
Joined: 21st Jul 2010
Location: Coos bay, rainville
Posted: 14th Sep 2010 21:11 Edited at: 14th Sep 2010 23:01
Heh, what luck! I don't have internet at the house at the moment, and this is exactly what I just got on for. Very helpful, thanks.
Suppose that you did want to be able to use integers and all of those good 'ol variables. What kind of file would you use?
Grog Grueslayer
Valued Member
19
Years of Service
User Offline
Joined: 30th May 2005
Playing: Green Hell
Posted: 15th Sep 2010 12:16
Quote: "Suppose that you did want to be able to use integers and all of those good 'ol variables. What kind of file would you use?"


There really is only one kind of file but if you use .txt as an extension it can and usually does get looked at by a text editor.

When we use OPEN TO WRITE it creates a file... what we put in that file is determined by us. If you use WRITE STRING it'll put in text. WRITE BYTE adds a byte that can only be a number between 0 and 255 that takes up 1 byte to save. WRITE WORD adds a word that can only be a number between 0 and 65,535 that takes up 2 bytes to save.

Here's the full list on the different datatypes:


Michael73
14
Years of Service
User Offline
Joined: 29th Jan 2010
Location:
Posted: 17th Sep 2010 13:02
Thank You for all your help guys .

Login to post a reply

Server time is: 2024-11-23 07:26:51
Your offset time is: 2024-11-23 07:26:51