1 registered members (AndrewAMD),
900
guests, and 9
spiders. |
Key:
Admin,
Global Mod,
Mod
|
|
|
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
OP
Expert
|
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 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
Expert
|
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
Senior Expert
|
Senior Expert
Joined: May 2002
Posts: 7,441
|
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
Expert
|
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: ventilator]
#328823
06/15/10 15:15
06/15/10 15:15
|
Joined: Feb 2008
Posts: 3,232 Australia
EvilSOB
Expert
|
Expert
Joined: Feb 2008
Posts: 3,232
Australia
|
Here is a couple Ive tested fully... efficiencies were only loosly tested...
//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
//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
OP
Expert
|
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
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():
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.
|
|
|
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
Senior Expert
|
Senior Expert
Joined: May 2002
Posts: 7,441
|
this is what i meant:
#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
OP
Expert
|
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.
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.
|
|
|
|