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 / Writing integers to .txt

Author
Message
NoUJoe
11
Years of Service
User Offline
Joined: 8th Jul 2013
Location:
Posted: 16th Oct 2013 22:38
So I'm just playing around with the file handling functions. I thought I'd use a .txt file for this. Why are integers written to the file as letters and symbols? I'm only presuming something to do with the way its encoded or something? Thanks in advance
=PRoF=
21
Years of Service
User Offline
Joined: 17th Mar 2003
Location: Milton Keynes, UK
Posted: 16th Oct 2013 22:58
It's because it's storing the integers as integers, and then you open it in Notepad or whatever and it converts those integers to ascii and displays them as gibberish.

What you are probably wanting to do is write the integers to a string,



Hope that helps...

Digital Awakening
AGK Developer
22
Years of Service
User Offline
Joined: 27th Aug 2002
Location: Sweden
Posted: 16th Oct 2013 22:59 Edited at: 16th Oct 2013 23:00
You must convert the numbers to strings using str() and the write them as strings to be readable as numbers. Otherwise the text program will not be able to interpret your numbers as numbers. IIRC one character is usually only 8 bit (1 byte, 0-255) and an integer is usually 4 bytes (a much larger value range)

Ninjaed

NoUJoe
11
Years of Service
User Offline
Joined: 8th Jul 2013
Location:
Posted: 16th Oct 2013 23:42
Thanks for the replies. I'm not so bothered about it being readable. More if when I read the .txt file back in. Will it still read the integers as integers? I've just put two and two together, so when you open, lets say a .sav file, with notepad and its loads of random numbers/letters/symbols, the reason is what you guys just explained?
=PRoF=
21
Years of Service
User Offline
Joined: 17th Mar 2003
Location: Milton Keynes, UK
Posted: 16th Oct 2013 23:58 Edited at: 16th Oct 2013 23:59
Yes, if you write an integer to a file, you can read it back in as an integer and it'll be the same value as you wrote. You can read/write floats as well.

And yes, you're right Notepad converts anything it opens to text and attempts to display it as such. Because it was saved as data and not text, all you see is random characters.

Ancient Lady
Valued Member
20
Years of Service
User Offline
Joined: 17th Mar 2004
Location: Anchorage, Alaska, USA
Posted: 17th Oct 2013 00:00
If you use the 'Write' commands other than 'WriteLine', it outputs the values as binary (not human readable and generally less space used).

To read them back you use the 'Read' commands. They need to be read using the same type as written and in the same order. If you use a WriteByte and WriteFloat and then try to read using ReadInteger and ReadFloat, you won't get the correct values.

And the extension of the file has no meaning at all for AppGameKit (at least it shouldn't).

If you did want it to be all human readable then you use only the WriteLine command and the Str command to convert numbers to strings before you use the WriteLine. And then you read it back with ReadLine, storing it into a string variable, and do any conversions on the results that you want.

Cheers,
Ancient Lady
AGK Community Tester and AppGameKit Master

Login to post a reply

Server time is: 2024-11-24 23:12:40
Your offset time is: 2024-11-24 23:12:40