0 registered members (),
1,209
guests, and 0
spiders. |
Key:
Admin,
Global Mod,
Mod
|
|
|
Re: Reading int or float values from a file
[Re: EvilSOB]
#340512
09/04/10 20:38
09/04/10 20:38
|
Joined: Jul 2010
Posts: 127 Germany, Herford
Ditje
OP
Member
|
OP
Member
Joined: Jul 2010
Posts: 127
Germany, Herford
|
Hi, I am back - and my problems, too. @EvilSOB - I have tested it and it works. But I recognized, that it`s not enough. Of course, I will create an original higscore list and I forgot the player names. So only a value is not enough - THX. @JustSid - You script is still, that what I want and still with "Kanonen auf Spatzen". I took a look to a few Source-Files and I am sure, I will need maximum one or two of them. There is so much stuff to be installed, which is not needed and what I don`t know what it does.
|
|
|
Re: Reading int or float values from a file
[Re: Ditje]
#340515
09/04/10 21:11
09/04/10 21:11
|
Joined: Apr 2007
Posts: 3,751 Canada
WretchedSid
Expert
|
Expert
Joined: Apr 2007
Posts: 3,751
Canada
|
I'm sorry, but there is no Lite Foundation script file that isn't needed for my high score example =/ There is so much stuff to be installed, which is not needed and what I don`t know what it does.
Uhm, you don't need to install anything to use Lite Foundation and like I said, all things are used (but mostly transparent in the background). However, Lite Foundation does nothing that it shouldn't and it should be safe to use it.
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]
#340521
09/04/10 23:17
09/04/10 23:17
|
Joined: Feb 2008
Posts: 3,232 Australia
EvilSOB
Expert
|
Expert
Joined: Feb 2008
Posts: 3,232
Australia
|
Try this then... And its designed to work with DOUBLES instead of floats.
#include <litec.h>
#include <default.c>
#define PRAGMA_PATH "@Bits"
void main()
{
STRING* highscore_name = str_create("player name");
double highscore = 1000000000;
// highscore save
var highscore_handle;
highscore_handle = file_open_write("highscores.txt");
file_str_write(highscore_handle, highscore_name );
file_str_write(highscore_handle, ",");
file_str_writeto(highscore_handle, (char*)&highscore, 8 );
file_close(highscore_handle);
STRING* highscore2_name = str_create("");
double highscore2 = 0;
// highscore load
var highscore_handle; STRING* tmp = str_create("#9");
highscore_handle = file_open_read ("highscores.txt");
file_str_read(highscore_handle, highscore2_name);
file_chr_read(highscore_handle, tmp); memcpy(&highscore2, tmp.chars, 8);
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(highscore_name, 180,10, vector(100,100,100));
draw_text(tmp1, 300,10, vector(100,100,100));
draw_text("Score LOADED =", 10,40, vector(100,100,100));
draw_text(highscore2_name, 180,40, vector(100,100,100));
draw_text(tmp2, 300,40, vector(100,100,100));
wait(1);
}
}
"There is no fate but what WE make." - CEO Cyberdyne Systems Corp. A8.30.5 Commercial
|
|
|
Re: Reading int or float values from a file
[Re: EvilSOB]
#340542
09/05/10 13:36
09/05/10 13:36
|
Joined: Jul 2010
Posts: 127 Germany, Herford
Ditje
OP
Member
|
OP
Member
Joined: Jul 2010
Posts: 127
Germany, Herford
|
I am getting on. If I use file_open_append instead of file_open_write, I can add new entries to the file. DOUBLES seems to be the only possiblity. I checked other types, too If I take a look to the textfile the DOUBLE values are crypted. I understood that it`s done by file_str_writeto(highscore_handle, (char*)&highscore, 8 ); and "redone" by file_chr_read(highscore_handle, tmp); memcpy(&highscore2, tmp.chars, 8); Can you please explain what happens? THX Ditje @Sid - it`s nothing about trust I have send you a PM.
|
|
|
Re: Reading int or float values from a file
[Re: Ditje]
#340547
09/05/10 14:11
09/05/10 14:11
|
Joined: Feb 2008
Posts: 3,232 Australia
EvilSOB
Expert
|
Expert
Joined: Feb 2008
Posts: 3,232
Australia
|
No problem.
file_str_writeto(highscore_handle, (char*)&highscore, 8 ); "file_str_writeto" is meant for writing STRING or char* data. So by telling it to write "(char*)&highscore, 8", I have told it that there is a char* block at the same address as the double we want to send, and that the block is 8 bytes long (the size of a double). So it then writes the 8 bytes of our double as an 8 byte 'string'.
file_chr_read(highscore_handle, tmp); memcpy(&highscore2, tmp.chars, 8); "file_chr_read" reads char* data into a string. So I create "tmp" 9 bytes long, which is one byte longer than the length of a double. The extra byte is to allow for the null character (created by the "file_str_writeto") in the file. So our data is read out of the file and stored in the "tmp" string. I then use memcpy to copy that data (from the tmp.chars location) and store it in our finishing double variable. The "tmp" string is no longer needed, so it can now be re-used if you are going to be reading more doubles.
"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
|