Gamestudio Links
Zorro Links
Newest Posts
Executing Trades on Next Bar Open
by vicknick. 06/13/24 08:51
Zorro Beta 2.61: PyTorch
by jcl. 06/10/24 14:42
New FXCM FIX Plugin
by flink. 06/04/24 07:30
AlpacaZorroPlugin v1.3.0 Released
by kzhao. 05/22/24 13:41
Free Live Data for Zorro with Paper Trading?
by AbrahamR. 05/18/24 13:28
AUM Magazine
Latest Screens
The Bible Game
A psychological thriller game
SHADOW (2014)
DEAD TASTE
Who's Online Now
1 registered members (AndrewAMD), 900 guests, and 9 spiders.
Key: Admin, Global Mod, Mod
Newest Members
AemStones, LucasJoshua, Baklazhan, Hanky27, firatv
19058 Registered Users
Previous Thread
Next Thread
Print Thread
Rate Thread
Page 2 of 3 1 2 3
Re: Number of elements in an array? [Re: EvilSOB] #328795
06/15/10 12:10
06/15/10 12:10
Joined: Mar 2003
Posts: 3,010
analysis paralysis
NITRO777 Offline OP
Expert
NITRO777  Offline OP
Expert

Joined: Mar 2003
Posts: 3,010
analysis paralysis
Hi EvilSOB,
Thanks for the help, I haven't quite been able to try your methods yet because my problem has grown more complicated to the point where I dont knopw if I can even explain it online grin

Basically I used Helghast's method to count spaces with file_find and that worked, but for some reason it was incredibly slow, taking up to a minute to go through a 5000 number list. It also became difficult for me because I am actually reading multiple files at once.

So right now I have some larger issues to contend with. I do have to say that your second way definitely looks like it is in the right direction except for the fact that I may have misrepresented my data. I dont actually know the range, I was only posting those exampkles as to the variables types and format of my numbers.

Also your first method looks ok too, but isnt file_length strictly for judging file lengths in terms of bytes?

Anyway, as I said, I have to deal with the issue of how to seperate my queries for each individual file because I am reading multiple files at once, so I have larger issues right now

Thanks very much for the help

Re: Number of elements in an array? [Re: NITRO777] #328811
06/15/10 14:06
06/15/10 14:06
Joined: Feb 2008
Posts: 3,232
Australia
EvilSOB Offline
Expert
EvilSOB  Offline
Expert

Joined: Feb 2008
Posts: 3,232
Australia
file_length IS length of file in bytes, but thats cool, because YOU are not going to use it for anything.

'file_length' is only used to compare with 'file_seek', which is also in bytes.
ALL it is doing is checking to see if youve hit the physical end of the file.
Once that happens, it stops reading by exiting the loop, and you then use the
value accumulated in "var_counter" to see how many actual numbers were retrieved...

Forget the second version if you dont have any limits on the data ranges.

I dont get where there is a problem handling multiple files either.
Are they all READ-ONLY text files?
Or are they somewhat dynamic, being created on-the-fly by another part of the program?


"There is no fate but what WE make." - CEO Cyberdyne Systems Corp.
A8.30.5 Commercial
Re: Number of elements in an array? [Re: EvilSOB] #328812
06/15/10 14:21
06/15/10 14:21
Joined: May 2002
Posts: 7,441
ventilator Offline
Senior Expert
ventilator  Offline
Senior Expert

Joined: May 2002
Posts: 7,441
Quote:
Returns the number that was written into the file with the given handle, and proceeds to the next position within the file. The file must be open for reading. If there are no further numbers, 0 is returned.
hm... maybe i am misunderstanding something but can't you simply run a loop until file_var_read() returns 0?

Re: Number of elements in an array? [Re: ventilator] #328814
06/15/10 14:28
06/15/10 14:28
Joined: May 2005
Posts: 2,713
Lübeck
Slin Offline
Expert
Slin  Offline
Expert

Joined: May 2005
Posts: 2,713
Lübeck
As mentioned before ventilator, it will then stop the loop early, if there are zeros in the file.
So file_seek is probably the best approach.

Last edited by Slin; 06/15/10 14:29.
Re: Number of elements in an array? [Re: Slin] #328816
06/15/10 14:33
06/15/10 14:33
Joined: May 2002
Posts: 7,441
ventilator Offline
Senior Expert
ventilator  Offline
Senior Expert

Joined: May 2002
Posts: 7,441
oh, that's stupid. tongue i didn't check file_var_read() in detail and thought the return value is separate from the read number. i think i had fscanf() in mind. i guess it would do the job?

Re: Number of elements in an array? [Re: ventilator] #328823
06/15/10 15:15
06/15/10 15:15
Joined: Feb 2008
Posts: 3,232
Australia
EvilSOB Offline
Expert
EvilSOB  Offline
Expert

Joined: Feb 2008
Posts: 3,232
Australia
Here is a couple Ive tested fully... efficiencies were only loosly tested...
Code:
//most efficient on files larger than 10kb
var file_var_count_0(STRING* filename)
{	if(!filename)		return(-1);		//ERROR, not valid filename
	long file_len=0, pos, count=0;
	byte* data = file_load(_chr(filename), NULL, &file_len);
	if(!data)			return(-1);		//ERROR, file not found
	for(pos=0; pos<file_len; pos++)		if((long)data[pos]==32)		count++;
	file_load(NULL, data, NULL);
	return(count);
}


and
Code:
//most efficient on files smaller than 50kb
var file_var_count_1(STRING* filename)
{	if(!filename)		return(-1);		//ERROR, not valid filename
	var count=0, file=file_open_read(filename);
	if(!file)			return(-1);		//ERROR, file not found
	while(file_find(file, " ")>=0)	count++;
	file_close(file);	return((var)count);
}




"There is no fate but what WE make." - CEO Cyberdyne Systems Corp.
A8.30.5 Commercial
Re: Number of elements in an array? [Re: ventilator] #328829
06/15/10 15:30
06/15/10 15:30
Joined: Mar 2003
Posts: 3,010
analysis paralysis
NITRO777 Offline OP
Expert
NITRO777  Offline OP
Expert

Joined: Mar 2003
Posts: 3,010
analysis paralysis
Wow, thanks very much for the help. Let me just explain very briefly the structure of the program which I have hacked and slashed from george's aum 70 record/replay.

1. there are external .txt files with lists of numbers (you knew that already)

2. I have 2 files in my program, a main.c file and a play_me.c file(other than the default and scknex included files of course)

3. In the main.c file there is the action attached to models in the game in WED the action is
Code:
action entity_example()
{
	
   
	// these 3 lines have to be added to every entity that needs to be recorded
	max_ents += 1; // get a unique entity number 
   my.skill99 = max_ents; // and store it inside skill99 
	play(my.skill99); // put this line here
	
}



Thus each model added to the game and assigned this action will create its own identifying number stored in its my.skill99. Thats why I say that it is getting complex because therer can be several entitie reading several files. Each file is different and unique for each entity.


Now in the other program file, called play_me.c I will show you the function play_me():

Code:
function play(entity_number)
{
	my.emask |= ENABLE_ENTITY; 
  
   my.event = pause_event;
	STRING* rec_str = "       "; // stores the name of the recorded data file
	STRING* temp_str = "  "; // just a temporary string
	var filehandle;
   
	str_cpy(rec_str, str_for_num(temp_str, entity_number)); // create the name of the data file depending on entity's number
	str_cat(rec_str, ".txt"); // and then add it the ".txt" extension
	filehandle = file_open_read(rec_str); // now we can try to open the file

		var temp = 0;
	   VECTOR tempStore;
      VECTOR tempStorePan;
      VECTOR tempVector;
	
		   while(temp < 3500)
		   {
		 	   tempStore.x=file_var_read(filehandle);
			  	tempStore.y=file_var_read(filehandle);
			   tempStore.z=file_var_read(filehandle);
			   tempStorePan.x=file_var_read(filehandle);
			   tempStorePan.y=file_var_read(filehandle);
			   tempStorePan.z=file_var_read(filehandle);
			   my.frame = file_var_read(filehandle); // and put them inside the array

			   // rotate towards target
				vec_set(tempVector, tempStore.x);
				vec_sub(tempVector, my.x);
				vec_to_angle(my.pan, tempVector);
				
				//move to it with collision
			   c_move(my, vector(5 * time_step, 0, 0), nullvector, IGNORE_PASSABLE | GLIDE);

			 temp +=1;
			 wait(1);
			  
	      }
	      
	
		file_close(filehandle); // close the file
		beep();
		
    	
	
}




As you can see in the above example I am just using an arbitrary number 3500 because I havent yet found a way to use a different counter for each entity dependent upon it's identifier.

So thank you EvilSOB I will now look at the file_seek method, thank you for explaining it to me. I think the reason I was having trouible reading concurrent files was because of the way I structured the while loop.

I admit that sometimes things get very confusing for me and I end up concentrating on something irrelevant or missing some small detail.

Anyway, thanks veru much for the help thus far. crazy

Re: Number of elements in an array? [Re: NITRO777] #328831
06/15/10 15:32
06/15/10 15:32
Joined: Mar 2003
Posts: 3,010
analysis paralysis
NITRO777 Offline OP
Expert
NITRO777  Offline OP
Expert

Joined: Mar 2003
Posts: 3,010
analysis paralysis
Oh ok I was writing this last post while you were posting that code blush, I will look at implementing that now...

Re: Number of elements in an array? [Re: NITRO777] #328835
06/15/10 15:48
06/15/10 15:48
Joined: May 2002
Posts: 7,441
ventilator Offline
Senior Expert
ventilator  Offline
Senior Expert

Joined: May 2002
Posts: 7,441
this is what i meant:

Code:
#include <acknex.h>
#include <stdio.h>

void main()
{
	float f;
	FILE *file = fopen("numbers.txt", "r");
	while(fscanf(file, "%f", &f) == 1)
	{
		printf("%f", (double)f);
	}				
	fclose(file);
}



no need to do any counting beforehand. it also is very forgiving with newlines and whitespace.

Re: Number of elements in an array? [Re: NITRO777] #328839
06/15/10 15:54
06/15/10 15:54
Joined: Mar 2003
Posts: 3,010
analysis paralysis
NITRO777 Offline OP
Expert
NITRO777  Offline OP
Expert

Joined: Mar 2003
Posts: 3,010
analysis paralysis
Hi EvilSOB,

I implemented your code as you can see below. This way of using file_find is nearly exactly how I did it after following Helghasts advice. However there seems to be a problem having that while loop occur before the second while loop.

The models dont follow the path they just go straight to the origin. So thats the problem I guess. It is definitely copunting the right number of words in the file, so that much is working correctly, but the subsequent while loop gets screwed up somehow as a result and I have no idea why.

Code:
function play(entity_number)
{
	my.emask |= ENABLE_ENTITY; 
  
   my.event = pause_event;
	STRING* rec_str = "       "; // stores the name of the recorded data file
	STRING* temp_str = "  "; // just a temporary string
	var filehandle;
   
	str_cpy(rec_str, str_for_num(temp_str, entity_number)); // create the name of the data file depending on entity's number
	str_cat(rec_str, ".txt"); // and then add it the ".txt" extension
	filehandle = file_open_read(rec_str); // now we can try to open the file

		var temp = 0;
	   VECTOR tempStore;
      VECTOR tempStorePan;
      VECTOR tempVector;
      
      var spaceFind;
      var totalSpaces=0;
      
		var count=0;
		//file=file_open_read(filename);
		//if(!file)			return(-1);		//ERROR, file not found
		while(file_find(filehandle, " ")>=0)	count++;
      globalTester = count;//to watch the variable
         	
         
		   while(temp < count)
		   {
		 	   tempStore.x=file_var_read(filehandle);
			  	tempStore.y=file_var_read(filehandle);
			   tempStore.z=file_var_read(filehandle);
			   tempStorePan.x=file_var_read(filehandle);
			   tempStorePan.y=file_var_read(filehandle);
			   tempStorePan.z=file_var_read(filehandle);
			   my.frame = file_var_read(filehandle); // and put them inside the array

			   // rotate towards target
				vec_set(tempVector, tempStore.x);
				vec_sub(tempVector, my.x);
				vec_to_angle(my.pan, tempVector);
				
				//move to it with collision
			   c_move(my, vector(5 * time_step, 0, 0), nullvector, IGNORE_PASSABLE | GLIDE);

			 temp +=1;
			 wait(1);
			  
	      }
	      
	
		file_close(filehandle); // close the file
		beep();
		
    	
	
}



Last edited by NITRO777; 06/15/10 16:00.
Page 2 of 3 1 2 3

Moderated by  HeelX, Lukas, rayp, Rei_Ayanami, Superku, Tobias, TWO, VeT 

Gamestudio download | chip programmers | Zorro platform | shop | Data Protection Policy

oP group Germany GmbH | Birkenstr. 25-27 | 63549 Ronneburg / Germany | info (at) opgroup.de

Powered by UBB.threads™ PHP Forum Software 7.7.1