0 registered members (),
1,209
guests, and 0
spiders. |
Key:
Admin,
Global Mod,
Mod
|
|
|
Reading int or float values from a file
#339114
08/24/10 18:06
08/24/10 18:06
|
Joined: Jul 2010
Posts: 127 Germany, Herford
Ditje
OP
Member
|
OP
Member
Joined: Jul 2010
Posts: 127
Germany, Herford
|
Hi all, I have already searched for and expected to find anything, but nothing? I just wanted to write a highscore function in a few minutes - no chance I have a higscore txt-file (highscores.txt) with the content 10000,9000,8000,...,1000 The only function which could handle my plan seems to be "file_var_read". But if I try the following code var highscore gets no value, even when the file contains only one value. So how can I get int or float values from a text file? BTW: Good to know for later. Is it possible to read .csv? Thx Ditje
// highsocre laden
var highscore;
var highscore_handle;
highscore_handle = file_open_read ("highscores.txt");
highscore = file_var_read(highscore_handle);
file_close(highscore_handle);
|
|
|
Re: Reading int or float values from a file
[Re: Ditje]
#339115
08/24/10 18:14
08/24/10 18:14
|
Joined: Apr 2007
Posts: 3,751 Canada
WretchedSid
Expert
|
Expert
Joined: Apr 2007
Posts: 3,751
Canada
|
sprintf from stdio.h will help you. Edit: Argh, sorry, it won't help you. Thought into the wrong direction However, you could use Lite Foundation to create an array with the highscore and then serialize the array and save it into the file. Ist aber die Sprichwörtliche "Kanone auf Spatzen"
And you can read whatever you want, the suffix gives you just a pointer of what you might find inside the file. But often you need to write a parser for the specific file type
Last edited by JustSid; 08/24/10 18:21.
Shitlord by trade and passion. Graphics programmer at Laminar Research. I write blog posts at feresignum.com
|
|
|
Re: Reading int or float values from a file
[Re: WretchedSid]
#339117
08/24/10 18:28
08/24/10 18:28
|
Joined: Jul 2010
Posts: 127 Germany, Herford
Ditje
OP
Member
|
OP
Member
Joined: Jul 2010
Posts: 127
Germany, Herford
|
Hi Sid, I only understood "Bahnhof". Should I include stdio.h or copy the printf function from stdio.h? I took a look to the file and understood - nothing Manual example contains 2 values spilt by " " (space), but that doesn`t work for me, too. Cheers Ditje
|
|
|
Re: Reading int or float values from a file
[Re: Ditje]
#339119
08/24/10 18:34
08/24/10 18:34
|
Joined: Apr 2007
Posts: 3,751 Canada
WretchedSid
Expert
|
Expert
Joined: Apr 2007
Posts: 3,751
Canada
|
Sorry, sprintf isn't the function you are looking for. I thought that you wanted to write the integers and floats... Forget the sprintf.
But if you want, I can write you a function that uses Lite Foundation to load and save a mutable highscore list.
Shitlord by trade and passion. Graphics programmer at Laminar Research. I write blog posts at feresignum.com
|
|
|
Re: Reading int or float values from a file
[Re: WretchedSid]
#339132
08/24/10 19:21
08/24/10 19:21
|
Joined: Jul 2010
Posts: 127 Germany, Herford
Ditje
OP
Member
|
OP
Member
Joined: Jul 2010
Posts: 127
Germany, Herford
|
Hey Sid, Hmm do I want? - Yeah I would learn more intensive, if I do it by my own. But, if you do it, I can learn from your code, too. Thank you very much. Cheers Ditje
|
|
|
Re: Reading int or float values from a file
[Re: Ditje]
#339148
08/24/10 21:24
08/24/10 21:24
|
Joined: Apr 2007
Posts: 3,751 Canada
WretchedSid
Expert
|
Expert
Joined: Apr 2007
Posts: 3,751
Canada
|
Here is an example of an Highscore list with Lite Foundation. If you need help with Lite Foundation, here is a thread about the project: http://www.opserver.de/ubb7/ubbthreads.php?ubb=showflat&Number=338910#Post338910The example leaks a bit memory (the highscore array didn't get released), you can avoid this bei either manually releasing it or using an autorelease pool. Here is the link to the example: http://cl.ly/a8f1fe950d903bee9abeIt uses a slightly different Lite Foundation version than the one in the main thread, I needed to add the insertInto function for arrays! Have fun with the example, I hope it is self explaining enough
Shitlord by trade and passion. Graphics programmer at Laminar Research. I write blog posts at feresignum.com
|
|
|
Re: Reading int or float values from a file
[Re: WretchedSid]
#339179
08/25/10 07:25
08/25/10 07:25
|
Joined: Jul 2010
Posts: 127 Germany, Herford
Ditje
OP
Member
|
OP
Member
Joined: Jul 2010
Posts: 127
Germany, Herford
|
Good morning! I was earlier in bed than you Thank you very much for the script. I took a look to the files. Puh - 150 lines of code and 3 additional include files, just to read 10 interger and 10 string values? In my opinion there is a basic function missing in Gamestudio. I WANT this highscore list. In PHP I would have done in 5 minutes - in GS I have to do it after my vacancy. Be sure - I will raise this topic, when I am back Cheers Ditje
|
|
|
Re: Reading int or float values from a file
[Re: Ditje]
#339195
08/25/10 09:02
08/25/10 09:02
|
Joined: Apr 2007
Posts: 3,751 Canada
WretchedSid
Expert
|
Expert
Joined: Apr 2007
Posts: 3,751
Canada
|
Well, I don't think that there are missing functions in Lite-C, but like I said, the Lite Foundation solution is "mit der Kanone auf Spatzen". However, with Lite Foundation you get some nice feature for free, eg the highscore list length is only limited by your RAM. It shows only 10 because the TEXT has only 10 lines. Together with LBGUI you could create a nice scrollable list. The code could be also written shorter, the long thing is reading and writing the file, I needed to use the plain C solution via FILE because file_xxx_read and file_xxx_write can't handle binary data. The other long thing is the inserting into the array, this could be also made shorter by using LFDictionaryRef's. However, the aren't ported to Lite-C yet, so I needed to make the highscore array a bit longer saving first the name and then the score. Once the LFDictionaryRef is available for Lite-C, I can show you how to use them inside the array and how to sort an array of this kind (Lite Foundation offers you an easy way to sort any kind of arrays)
JustSid
Shitlord by trade and passion. Graphics programmer at Laminar Research. I write blog posts at feresignum.com
|
|
|
Re: Reading int or float values from a file
[Re: WretchedSid]
#339226
08/25/10 12:34
08/25/10 12:34
|
Joined: Feb 2008
Posts: 3,232 Australia
EvilSOB
Expert
|
Expert
Joined: Feb 2008
Posts: 3,232
Australia
|
Ive just put this together... Its APPEARING to be ok for floats, but NOT doubles. (Its not intended for doubles, bad concept) But I do get some discrepancy between the saved and read-back numbers. Is this expected of floats? Or something else....
#include <litec.h>
#include <default.c>
#define PRAGMA_PATH "@Bits"
void main()
{
// highscore save
float highscore = 1000000000;
var highscore_handle;
highscore_handle = file_open_write("highscores.txt");
file_var_write(highscore_handle, *((var*)&highscore) );
file_close(highscore_handle);
// highscore load
float highscore2 = 0;
var highscore_handle;
highscore_handle = file_open_read ("highscores.txt");
var tmp_var = file_var_read(highscore_handle);
highscore2 = *((float*)&tmp_var);
file_close(highscore_handle);
STRING* tmp1 = str_create(""); str_for_float(tmp1, highscore);
STRING* tmp2 = str_create(""); str_for_float(tmp2, highscore2);
while(1)
{
draw_text("Score SAVED =", 10,10, vector(100,100,100));
draw_text(tmp1, 180,10, vector(100,100,100));
draw_text("Score LOADED =", 10,40, vector(100,100,100));
draw_text(tmp2, 180,40, vector(100,100,100));
wait(1);
}
}
"There is no fate but what WE make." - CEO Cyberdyne Systems Corp. A8.30.5 Commercial
|
|
|
Moderated by mk_1, Perro, rayp, Realspawn, Rei_Ayanami, rvL_eXile, Spirit, Superku, Tobias, TSG_Torsten, VeT
|