Cout number of lines in file
#1

Hello,

I would like to know how can we get the number of lines in a file ?
I use the include "file".

Regards
Reply
#2

fread (second example with while loop) and increase a variable inside. After it's done, the variable will store the lines of a file.
Reply
#3

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?
Reply
#4

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.
Reply
#5

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
Reply
#6

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

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
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)