PDA

View Full Version : fgetarg and freadln



Ni3ls
26th October 2015, 17:32
Hi all,

We have 2 commands for reading scriptdata. But what is exactly the difference? With fgetarg you can read a certain argument, but what does freadln do exactly?

IzNoGoD
27th October 2015, 01:09
freadln just reads the amount of args on a line and skips to the next line after (first freadln doesnt actually skip to the next line, else you wouldnt be able to get the argcount on line 1 AND read the args)

Ni3ls
27th October 2015, 08:53
But how to use freadln properly? And is it even that usefull?

IzNoGoD
27th October 2015, 10:16
readfile(filename)
{
result = [];
fid = openfile(filename, "read");
if(fid == -1)
return undefined;
linenum = 0;
argcount = freadline(fid);
while(argcount > -1)
{
result[linenum] = [];
for(i = 0; i < argcount; i++)
result[linenum][result[linenum].size] = fgetarg(fid, i);
argcount = freadline(fid);
}
closefile(fid);
return result;
}

Ni3ls
28th October 2015, 14:37
So for example when I have a file like this

1234, 1245, 1236, 234645, 131233,
How can I read all those values and store them in an array?

IzNoGoD
28th October 2015, 14:49
readthatfile()
{
f = openfile("thatfile.txt", "read");
if(f == -1)
return;
argcount = freadln(f);
if(argcount == -1) //end of file
return;
result = [];
for(i = 0; i < argcount; i++)
{
result[i] = fgetarg(f, i);
}
closefile(f);
return result;
}

Ni3ls
28th October 2015, 15:35
Works perfectly!