Cout number of lines in file -
jcvag44800 - 05.05.2014
Hello,
I would like to know how can we get the number of lines in a file ?
I use the include "file".
Regards
Re: Cout number of lines in file -
Konstantinos - 05.05.2014
fread (second example with while loop) and increase a variable inside. After it's done, the variable will store the lines of a file.
Re : Cout number of lines in file -
jcvag44800 - 05.05.2014
Hello,
There is my code:
Код:
new Float:x, Float:y, Float:z;
new count = 0;
GetPlayerPos(playerid, x, y, z)
new File: file = fopen("cars.cfg", io_read);
if (file)
{
while(fread(file, string))
{
count++;
}
// fwrite(file, "%d,%d,%d,%d,%d,%d,%d,%s,%s,%d,,%d,%d);
CreateVehicle(idveh, x+5, y, z, 82.2873, 0, 1, -1);
format(string, 128, "\r\n %d,%d,%d,%d,82,1,1,Dealership,Test,5000,,0,0", idveh,x,y,z);
fwrite(file, string);
fclose(file);
}
else
{
SendClientMessage(playerid,COLOR_RED,"Impossible d'ouvrir le fichier cars.cfg"); // not possible to open the file
return 1;
}
But, nothing is written, normal?
Re: Cout number of lines in file -
Konstantinos - 06.05.2014
The above code won't be compiled because of these:
pawn Код:
fwrite(file, "%d,%d,%d,%d,%d,%d,%d,%s,%s,%d,,%d,%d);
and
pawn Код:
format(string, 128, "\r\n %d,%d,%d,%d,82,1,1,Dealership,Test,5000,,0,0", idveh,x,y,z);
and you don't use "count" anywhere.
Re : Cout number of lines in file -
jcvag44800 - 06.05.2014
Actualy, I just try to write in the file. But nothing is it...
I don't know why
The code has been compiled...
There is the new code:
http://pastebin.com/dWs7PhCm
Regards and thank you for your help
Re: Cout number of lines in file -
Konstantinos - 06.05.2014
You open the file with mode io_read. You count the lines and you try to write. You need first to close the file and open it with mode io_append or io_write (removes everything and writes from the first line) so you can write to a file.
But still, you never used count so what is the point of counting the lines?
EDIT: I read your thread here:
https://sampforum.blast.hk/showthread.php?tid=511564
Do you want to just write to the last line that's why you wanted to count the lines? That can be done by opening the file with mode io_append.
Re : Cout number of lines in file -
jcvag44800 - 06.05.2014
I want to try to write in the last lines, so yes I'm going to use io_append.
EDIT: It's work now i'm using io_append
!
Thank you a lot