Reading int or float values from a file

Posted By: Ditje

Reading int or float values from a file - 08/24/10 18:06

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 grin

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
Code:
// highsocre laden
var highscore;
var highscore_handle;
highscore_handle = file_open_read ("highscores.txt");
highscore = file_var_read(highscore_handle);
file_close(highscore_handle);


Posted By: WretchedSid

Re: Reading int or float values from a file - 08/24/10 18:14

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
Posted By: Ditje

Re: Reading int or float values from a file - 08/24/10 18:28

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 grin

Manual example contains 2 values spilt by " " (space), but that doesn`t work for me, too.

Cheers

Ditje
Posted By: WretchedSid

Re: Reading int or float values from a file - 08/24/10 18:34

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.
Posted By: Ditje

Re: Reading int or float values from a file - 08/24/10 19:21

Hey Sid,

Hmm do I want? - Yeah laugh 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
Posted By: Aku_Aku

Re: Reading int or float values from a file - 08/24/10 19:26

Use spaces instead of commas.
Posted By: WretchedSid

Re: Reading int or float values from a file - 08/24/10 21:24

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#Post338910
The 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/a8f1fe950d903bee9abe
It 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 laugh
Posted By: Ditje

Re: Reading int or float values from a file - 08/25/10 07:25

Good morning!

I was earlier in bed than you laugh

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 laugh

Cheers

Ditje
Posted By: WretchedSid

Re: Reading int or float values from a file - 08/25/10 09:02

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
Posted By: EvilSOB

Re: Reading int or float values from a file - 08/25/10 12:34

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....

Click to reveal..
Code:
#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);
	}

}


Posted By: Ditje

Re: Reading int or float values from a file - 09/04/10 20:38

Hi,

I am back - and my problems, too. laugh

@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.
Posted By: WretchedSid

Re: Reading int or float values from a file - 09/04/10 21:11

I'm sorry, but there is no Lite Foundation script file that isn't needed for my high score example =/

Quote:

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.
Posted By: EvilSOB

Re: Reading int or float values from a file - 09/04/10 23:17

Try this then... And its designed to work with DOUBLES instead of floats.

Click to reveal..
Code:
#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);
	}

}


Posted By: Ditje

Re: Reading int or float values from a file - 09/05/10 13:36

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 laugh 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? laugh

THX Ditje

@Sid - it`s nothing about trust laugh I have send you a PM.
Posted By: EvilSOB

Re: Reading int or float values from a file - 09/05/10 14:11

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.
Posted By: Ditje

Re: Reading int or float values from a file - 09/05/10 14:41

Thank you very much - very detailed. I will try now, to get the complete List into vars laugh

Ditje
© 2024 lite-C Forums