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 / [SOLVED] WriteInteger() -- writing multiple integers to same file?

Author
Message
Zandy
User Offline
Joined: 26th Nov 2021
Playing:
Posted: 15th Aug 2022 15:18
I've managed writing a single integer to file fine but is there a way to write more than one to the same file or is it necessary to
start again with calls to OpenToWrite, WriteInteger, CloseFile? An example would be most appreciated; as would an example of it's complementary Read functions. Please and thank you

The author of this post has marked a post as an answer.

Go to answer

pavel_sv_1982
9
Years of Service
User Offline
Joined: 9th Apr 2013
Location: Russia
Posted: 15th Aug 2022 15:24 Edited at: 15th Aug 2022 16:32
This post has been marked by the post author as the answer.
As far as I remember, when I myself used this command, you have to start recording from the very beginning.
So you can't go wrong.
and do not close the file until you write everything.
programming is a hobby, not a job.
P.S. in google and youtube - nikename pavelman- it is not me!
Virtual Nomad
Moderator
16
Years of Service
User Offline
Joined: 14th Dec 2005
Location: SF Bay Area, USA
Posted: 15th Aug 2022 16:44 Edited at: 15th Aug 2022 17:00
one way using CSV (comma-separated values) to build a string of 10 integers into a single line of data:

no idea what you're doing with the integers so i'm just feeding the data into a 10x10 array for demonstration purposes (extracting single values from each string):

have a play with it.

add: i just re-read the question and it seems you were opening a file, writing an integer, closing the file for every value that you wanted to write. with the code provided, it's obviously not required. just Open File, Write/Read everything, Close File.

then, again, i mis-read the question. if you want a single integer on each line, i could modify the code. just ask and sorry for assuming (and not having consumed enough coffee before going on this adventure)

Attachments

Login to view attachments
Zappo
Valued Member
17
Years of Service
User Offline
Joined: 27th Oct 2004
Location: In the post
Posted: 15th Aug 2022 17:17
You can write any number of integers to a file in one go. Here is an example which does the following:
- Opens a file to write (will overwrite an existing file if it exists).
- Write 50 integers, all held in an array called "stats".
- Close the file.

Virtual Nomad
Moderator
16
Years of Service
User Offline
Joined: 14th Dec 2005
Location: SF Bay Area, USA
Posted: 15th Aug 2022 17:25 Edited at: 15th Aug 2022 17:26
@zappo, is it just me or is WriteInteger() adding an "empty" line to the file?



i think i've experienced this before and it seems like a bug unless i'm doing something wrong?
Zandy
User Offline
Joined: 26th Nov 2021
Playing:
Posted: 15th Aug 2022 18:09
Thanks to all! Each of your answers provided useful insight. Lightning fast responses and robust detailed answers are why the agk forum is mvp
Virtual Nomad
Moderator
16
Years of Service
User Offline
Joined: 14th Dec 2005
Location: SF Bay Area, USA
Posted: 15th Aug 2022 18:22 Edited at: 15th Aug 2022 18:24
ur welcome, zandy.

i do tend to learn something myself when i partake in these exercises so, it's a win-win (-win-win-win...) for all of us

always forward!
adambiser
AGK Developer
7
Years of Service
User Offline
Joined: 16th Sep 2015
Location: US
Posted: 15th Aug 2022 21:51
@Virtual Nomad: You should check FileEOF immediately after Reading before adding to Data.
Virtual Nomad
Moderator
16
Years of Service
User Offline
Joined: 14th Dec 2005
Location: SF Bay Area, USA
Posted: 16th Aug 2022 17:40 Edited at: 16th Aug 2022 17:43
so:

...to read an unknown number of integers/lines/etc.

i can't believe i didn't know this since i don't think i've had issues when reading files as i've always done... i figured EOF had an ascii value like EOL where that last EOL would be replaced by (an ascii) EOF at the end of the last line in a given file somehow (via CloseFile()).

i've since read a little on EOF and think i understand why it doesn't work that way.

thanks for the lesson
Phaelax
DBPro Master
19
Years of Service
User Offline
Joined: 16th Apr 2003
Location: Metropia
Posted: 23rd Aug 2022 15:35
Instead of using DO:LOOP and relying on EXIT, use a control loop that's designed for exiting on a condition. WHILE is probably the better choice over REPEAT unless you can guarantee the file will always have at least something to read first. WHILE would protect you from empty files.


While:



Repeat:

Tiled TMX Importer V.2
XML Parser V.2
Base64 Encoder/Decoder
Purple Token - Free online hi-score database
Legend of Zelda
Pixel-Perfect Collision

"I like offending people, because I think people who get offended should be offended." - Linus Torvalds

adambiser
AGK Developer
7
Years of Service
User Offline
Joined: 16th Sep 2015
Location: US
Posted: 23rd Aug 2022 18:45 Edited at: 23rd Aug 2022 18:46
@Phaelax: FileEOF should be checked immediately after attempting a read command. Both code snippets you posted will add an extra, non-existent 0 to the end of Data.

We don't have a way to do loops like this:

Login to post a reply

Server time is: 2022-10-04 14:19:14
Your offset time is: 2022-10-04 14:19:14