strange problem with INI
#1

Alright, I'm trying to make a 'Public' that update vehicle in a file named 'vehicles.cfg', to brief here's the code

Код:
			if(strcmp(cmd, "/admingarerveh", true) == 0)
	   	    {
    	    	if(IsPlayerConnected(playerid))
	    		{
		        	new IDVEH = GetPlayerVehicleID(playerid);
	   	        	new Float:PosX, Float:PosY, Float:PosZ, Float:PosA;
		        	GetVehiclePos(IDVEH, PosX, PosY, PosZ);
		        	GetVehicleZAngle(IDVEH, PosA);
		        	CarSystem[IDVEH][cX] = PosX;
		        	CarSystem[IDVEH][cY] = PosY;
		        	CarSystem[IDVEH][cZ] = PosZ;
		        	CarSystem[IDVEH][cAngle] = PosA;
		        	UpdateVehicle(IDVEH);
				}
				return 1;
	   	    }
Код:
public UpdateVehicle(IDVEH)
{
    	new File: file2 = fopen("vehicles.cfg");
		new coordsstring[258];
		format(coordsstring, sizeof(coordsstring), "%d|%d|%f|%f|%f|%f|%d|%d|%d|%d|%d|%d|%s|%d|%d|%d|%d|%d|%d|%d|%d|%d|%s|%d|%d|%d|%d|%d|%d|%d|%d|%d|%d|%d|%d|%d|%d",
  		CarSystem[IDVEH][cModel],
     	CarSystem[IDVEH][cEssence],
     	CarSystem[IDVEH][cX],
        CarSystem[IDVEH][cY],
        CarSystem[IDVEH][cZ],
        CarSystem[IDVEH][cAngle],
	    CarSystem[IDVEH][cColor1],
	    CarSystem[IDVEH][cColor2],
	    CarSystem[IDVEH][cKM],
	    CarSystem[IDVEH][cObjective],
	    CarSystem[IDVEH][cDoorsLocked],
	    CarSystem[IDVEH][cVehicleType],
	    CarSystem[IDVEH][cPlaque],
	    CarSystem[IDVEH][cVehicleMaterials],
	    CarSystem[IDVEH][cVehicleCrack],
	    CarSystem[IDVEH][cVehiclePot],
	    CarSystem[IDVEH][cJantes],
	    CarSystem[IDVEH][cMetre],
	    CarSystem[IDVEH][cVehicleHealth],
	    CarSystem[IDVEH][cVehicleAlarm],
	    CarSystem[IDVEH][cVehicleOwned],
	    CarSystem[IDVEH][cDescription],
	    CarSystem[IDVEH][cOwner],
	    CarSystem[IDVEH][cNitro],
	    CarSystem[IDVEH][cLoad],
	    CarSystem[IDVEH][veh_int],
	    CarSystem[IDVEH][veh_world],
	    CarSystem[IDVEH][cPaintJob],
	    CarSystem[IDVEH][cHydro],
	    CarSystem[IDVEH][cPimped],
	    CarSystem[IDVEH][cPerma],
	    CarSystem[IDVEH][cRank],
	    CarSystem[IDVEH][cPrice],
	    CarSystem[IDVEH][cSpoiler],
	    CarSystem[IDVEH][cFbumper],
	    CarSystem[IDVEH][cRbumper]);
		fwrite(file2, coordsstring);
		fclose(file2);
}
PHP код:
410|100|2095.676269|-1817.695922|13.038349|272.177825|-1|-1|0|0|0|0|none|0|0|0|-1|12|1000|0|0|0|none|-1|0|1|-1|0|-1|-1|-1|0|0|-1|-1|-1|
414|100|2132.622070|-1869.314697|13.165164|359.999023|-1|-1|0|0|0|0|none|0|0|0|-1|0|1000|0|0|0|none|-1|0|0|1|-1|-1|-1|-1|0|0|-1|-1|-1
e.g: If I'm in the second line(veh ID 2), the change is gonna happen in the first line(always the same thing,even if there's more vehicles),I don't know where is the wrong, please any help/suggestion is welcome
Reply
#2

You need to use io_append, so you can add to the file, and not re-write it.
Код:
fopen(filename[], io_append);
Also, you must add "\r\n" to the end of each line, so a new line is actually created.
Код:
format(coordsstring, sizeof(coordsstring), "%d|%d|%f|%f|%f|%f|%d|%d|%d|%d|%d|%d|%s|%d|%d|%d|%d|%d|%d|%d|%d|%d|%s|%d|%d|%d|%d|%d|%d|%d|%d|%d|%d|%d|%d|%d|%d\r\n", ...
Reply
#3

Quote:
Originally Posted by Stinged
Посмотреть сообщение
You need to use io_append, so you can add to the file, and not re-write it.
Код:
fopen(filename[], io_append);
Also, you must add "\r\n" to the end of each line, so a new line is actually created.
Код:
format(coordsstring, sizeof(coordsstring), "%d|%d|%f|%f|%f|%f|%d|%d|%d|%d|%d|%d|%s|%d|%d|%d|%d|%d|%d|%d|%d|%d|%s|%d|%d|%d|%d|%d|%d|%d|%d|%d|%d|%d|%d|%d|%d\r\n", ...
I tried with the code but still not working, it create another line

PHP код:
410|100|2015.104492|-1807.023071|13.114258|90.029151|-1|-1|0|0|0|0|none|0|0|0|-1|13|1000|0|0|0|none|-1|1|-1|0|0|-1|-1|-1|0|0|-1|-1|-1|1|
414|100|2132.622070|-1869.314697|13.165164|359.999023|-1|-1|0|0|0|0|none|0|0|0|-1|0|1000|0|0|0|none|-1|0|0|1|-1|-1|-1|-1|0|0|-1|-1|-1|
414|100|2132.208007|-1869.288208|13.641092|0.000000|-1|-1|0|0|0|0|none|0|0|0|-1|0|1000|0|0|0|none|-1|0|1|-1|0|-1|-1|-1|0|0|-1|-1|-1
Reply
#4

If a line needs to be updated then generally the whole file needs to be rewritten from scratch. Unless you know exactly where you want to write as in that case you can use fseek() to move the cursor the right location. However this implies that the length of each line is the same and that's not the case here.

Also don't assume that any file is an INI. The INI syntax is very specific.
Reply
#5

Quote:
Originally Posted by Vince
Посмотреть сообщение
If a line needs to be updated then generally the whole file needs to be rewritten from scratch. Unless you know exactly where you want to write as in that case you can use fseek() to move the cursor the right location. However this implies that the length of each line is the same and that's not the case here.

Also don't assume that any file is an INI. The INI syntax is very specific.
I tried with what you said, but nothing happens

Код:
public UpdateVehicle()
{
	for(new IDVEH = 1; IDVEH < MAX_VEHICLES; IDVEH++)
	{
    	new File: file2 = fopen("vehicles.cfg", io_readwrite);
		new coordsstring[258];
		format(coordsstring, sizeof(coordsstring), "%d|%d|%f|%f|%f|%f|%d|%d|%d|%d|%d|%d|%s|%d|%d|%d|%d|%d|%d|%d|%d|%d|%s|%d|%d|%d|%d|%d|%d|%d|%d|%d|%d|%d|%d|%d|%d",
  		CarSystem[IDVEH][cModel],
     	CarSystem[IDVEH][cEssence],
     	CarSystem[IDVEH][cX],
        CarSystem[IDVEH][cY],
        CarSystem[IDVEH][cZ],
        CarSystem[IDVEH][cAngle],
	    CarSystem[IDVEH][cColor1],
	    CarSystem[IDVEH][cColor2],
	    CarSystem[IDVEH][cKM],
	    CarSystem[IDVEH][cObjective],
	    CarSystem[IDVEH][cDoorsLocked],
	    CarSystem[IDVEH][cVehicleType],
	    CarSystem[IDVEH][cPlaque],
	    CarSystem[IDVEH][cVehicleMaterials],
	    CarSystem[IDVEH][cVehicleCrack],
	    CarSystem[IDVEH][cVehiclePot],
	    CarSystem[IDVEH][cJantes],
	    CarSystem[IDVEH][cMetre],
	    CarSystem[IDVEH][cVehicleHealth],
	    CarSystem[IDVEH][cVehicleAlarm],
	    CarSystem[IDVEH][cVehicleOwned],
	    CarSystem[IDVEH][cDescription],
	    CarSystem[IDVEH][cOwner],
	    CarSystem[IDVEH][cNitro],
	    CarSystem[IDVEH][cLoad],
	    CarSystem[IDVEH][veh_int],
	    CarSystem[IDVEH][veh_world],
	    CarSystem[IDVEH][cPaintJob],
	    CarSystem[IDVEH][cHydro],
	    CarSystem[IDVEH][cPimped],
	    CarSystem[IDVEH][cPerma],
	    CarSystem[IDVEH][cRank],
	    CarSystem[IDVEH][cPrice],
	    CarSystem[IDVEH][cSpoiler],
	    CarSystem[IDVEH][cFbumper],
	    CarSystem[IDVEH][cRbumper]);
		fwrite(file2, coordsstring);
		fclose(file2);
	}
	return 1;
}
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)