Stock creates the file but can't read it
#1

Hello I've been wasting so much time trying to fix this issue but I just couldn't come with a solution.
Alright I've kinda made a dealership system the same as one I found on a samp topic and everything was fine the system is dynamic so I can make dealership cars using /createdscar anywhere I want to but the issue is that the stock doesn't load the file it created, when I use the SaveDealershipCars() the file gets created and it saves the cars but in the 31th line for some reason and when I try to reopen the server, it doesn't load..no errors are actually on the script.

Код:
stock LoadDealershipCars()
{
	new dcar[7][32];
	new string[256], vehicle;
	new File:file = fopen("dealershipcars.cfg", io_read);
	if(file)
	{
	    new i = 1;
		while(i < MAX_DVEHICLES)
		{
		    fread(file, string);
		    split(string, dcar, '|');
			VehicleStatistics[i][vehicle_model] = strval(dcar[0]);
			VehicleStatistics[i][vehicle_price] = strval(dcar[1]);
   			VehicleStatistics[i][vehicle_position][1] = floatstr(dcar[2]);
		        VehicleStatistics[i][vehicle_position][2] = floatstr(dcar[3]);
		        VehicleStatistics[i][vehicle_position][3] = floatstr(dcar[4]);
			VehicleStatistics[i][vehicle_position][4] = floatstr(dcar[5]);
      		        VehicleStatistics[i][vehicle_onsale] = strval(dcar[6]);
			vehicle = CreateVehicle(VehicleStatistics[i][vehicle_model], VehicleStatistics[i][vehicle_position][1], VehicleStatistics[i][vehicle_position][2], VehicleStatistics[i][vehicle_position][3], VehicleStatistics[i][vehicle_position][4], 0, 0, -1);
			format(string, sizeof(string), "{91D593}This Vehicle is for sale\nModel:%s\nPrice:$%d\n{FFBE63}Enter the vehicle to buy it", GetVehicleName(vehicle), VehicleStatistics[i][vehicle_price]);
			VehicleStatistics[i][vehicle_label] = Create3DTextLabel(string, COLOR_BDO, VehicleStatistics[i][vehicle_position][1], VehicleStatistics[i][vehicle_position][2], VehicleStatistics[i][vehicle_position][3], 10.0, 0);
			i++;
	    }
	}
	print("Dealership Cars loaded successfully.");
	return 1;
}
Код:
stock SaveDealershipCars()
{
	new i = 1, File:file;
	new string[256];
	while(i < MAX_DVEHICLES)
	{
	    format(string, sizeof(string), "%d|%d|%f|%f|%f|%f|%d\r\n",
        VehicleStatistics[i][vehicle_model],
		VehicleStatistics[i][vehicle_price],
		VehicleStatistics[i][vehicle_position][1],
		VehicleStatistics[i][vehicle_position][2],
		VehicleStatistics[i][vehicle_position][3],
		VehicleStatistics[i][vehicle_position][4],
	    VehicleStatistics[i][vehicle_onsale]);
	    if(i == 1)
	    {
	        file = fopen("dealershipcars.cfg", io_write);
	    }
	    else
	    {
	    	file = fopen("dealershipcars.cfg", io_append);
	    }
		fwrite(file, string);
		fclose(file);
		i++;
	}
	print("Dealership Cars saved successfully.");
}
Server log:
Код:
[14:31:47] Houses loaded successfully.
// Dealership Cars loaded successfully. "should appear here but it doesn't"
[14:31:47] ----------------------------------
[14:31:47] |      My Roleplay Server      |
[14:31:47] ----------------------------------
[14:31:47] |      Created by Adam Hardy - Penguin       |
[14:31:47] ----------------------------------
[14:31:47] Number of vehicle models: 6
Reply
#2

When the server loads for the first time when the dealershipcars.cfg file doesn't exist it creates it and when I save the server it saves the cars in the 31th line then when I restart the server it doesn't load these cars and the stock at all.
Reply
#3

You are mainly wasting your time by trying to use a completely outdated method.

And then there are a couple of mistakes:

A) You don't close the file in your "load" function which means that handle still exists somewhere and the file supposedly remains open and locked.

B) Opening and closing the file for every line isn't needed and puts a lot of strain on the server. Open once (in write mode), write everything, close one.

C) The common method of reading a file line by line is using the construct while(fread(file, string)). You can then add other conditions by using logic operators:
PHP код:
while(fread(filestring) && MAX_DVEHICLES
This ensures that you don't fruitlessly try to read beyond the end of the file

D) Use sscanf to make your life easier.
Reply
#4

Thanks for your reply, I've used the function you just told me on both loading and saving the server crashed for me when I tried to save and when it didn't even load dealerships at all, these are the updated lines.

Код:
stock LoadDealershipCars()
{	        

	new dcar[7][32];
	new string[256], vehicle;
	new File:file = fopen("dealershipcars.cfg", io_read);
	if(file)
	{
	    new i = 1;
    	while(fread(file, string) && i < MAX_DVEHICLES)
		{
		    split(string, dcar, '|');
			VehicleStatistics[i][vehicle_model] = strval(dcar[0]);
			VehicleStatistics[i][vehicle_price] = strval(dcar[1]);
   			VehicleStatistics[i][vehicle_position][1] = floatstr(dcar[2]);
		    VehicleStatistics[i][vehicle_position][2] = floatstr(dcar[3]);
		    VehicleStatistics[i][vehicle_position][3] = floatstr(dcar[4]);
			VehicleStatistics[i][vehicle_position][4] = floatstr(dcar[5]);
      		VehicleStatistics[i][vehicle_onsale] = strval(dcar[6]);
			vehicle = CreateVehicle(VehicleStatistics[i][vehicle_model], VehicleStatistics[i][vehicle_position][1], VehicleStatistics[i][vehicle_position][2], VehicleStatistics[i][vehicle_position][3], VehicleStatistics[i][vehicle_position][4], 0, 0, -1);
			format(string, sizeof(string), "{91D593}This Vehicle is for sale\nModel:%s\nPrice:$%d\n{FFBE63}Enter the vehicle to buy it", GetVehicleName(vehicle), VehicleStatistics[i][vehicle_price]);
			VehicleStatistics[i][vehicle_label] = Create3DTextLabel(string, COLOR_BDO, VehicleStatistics[i][vehicle_position][1], VehicleStatistics[i][vehicle_position][2], VehicleStatistics[i][vehicle_position][3], 10.0, 0);
			i++;
	    }
     	fclose(file);
	}
	print("Dealership Cars loaded successfully.");
	return 1;
}
Код:
stock SaveDealershipCars()
{
	new i = 0, File:file;
	new string[256];
   	while(fread(file, string) && i < MAX_DVEHICLES)
	{
	    format(string, sizeof(string), "%d|%d|%f|%f|%f|%f|%d\r\n",
        VehicleStatistics[i][vehicle_model],
		VehicleStatistics[i][vehicle_price],
		VehicleStatistics[i][vehicle_position][1],
		VehicleStatistics[i][vehicle_position][2],
		VehicleStatistics[i][vehicle_position][3],
		VehicleStatistics[i][vehicle_position][4],
	    VehicleStatistics[i][vehicle_onsale]);
	    if(i == 0)
	    {
	        file = fopen("dealershipcars.cfg", io_write);
	    }
	    else
	    {
	    	file = fopen("dealershipcars.cfg", io_append);
	    }
		fwrite(file, string);
		i++;
	}
	fclose(file);
	print("Dealership Cars saved successfully.");
}
If you could please elaborate the functions you used in my code because when I tried them it just seemed like I had the same result
Reply
#5

Also I'll be using sscanf once I finish this server so no worries, sorry for taking you back to the old days lol.
Reply
#6

I have scripted a y_ini version of this system because the method you are using is very outdated and slow.

The system I have scripted will print out the amount of cars loaded and will also use seperate files for every vehicle.

Be sure to create a folder named "Dealership" in your scriptfiles folder. The data will be saved in it.

If you don't want to lose the cars you created with your old system, load them with your old system and save them with this system. After you saved them, import the new loading system in your script, and start the server.

Here's the code:

PHP код:
#include <YSI/y_ini> // On the top of your script
stock LoadDealershipCars()
{
    new 
string[144], count 0;
    
    for(new 
0MAX_DVEHICLESi++) // Loop the vehicles
    
{
        
format(stringsizeof(string), "Dealership/Car_%d.ini"i); // Format the string with the car's ID
        
        
if(fexist(string))
        {
            
INI_ParseFile(string"LoadVehicle_%s", .bExtra true, .extra i); // Loads the vehicle's data from the file
               
            
vehicle CreateVehicle(VehicleStatistics[i][vehicle_model], VehicleStatistics[i][vehicle_position][1], VehicleStatistics[i][vehicle_position][2], VehicleStatistics[i][vehicle_position][3], VehicleStatistics[i][vehicle_position][4], 00, -1);
            
format(stringsizeof(string), "{91D593}This Vehicle is for sale\nModel:%s\nPrice:$%d\n{FFBE63}Enter the vehicle to buy it"GetVehicleName(vehicle), VehicleStatistics[i][vehicle_price]);
            
VehicleStatistics[i][vehicle_label] = Create3DTextLabel(stringCOLOR_BDOVehicleStatistics[i][vehicle_position][1], VehicleStatistics[i][vehicle_position][2], VehicleStatistics[i][vehicle_position][3], 10.00);
            
count++;
        }
    }
    
printf("Loaded %d dealership cars."count); // Will print the number of cars loaded
    
return 1;
}
forward LoadVehicle_data(vehicleid,name[],value[]);
public 
LoadVehicle_data(vehicleid,name[],value[])
{
    
INI_Int(File"Model"VehicleStatistics[vehicleid][vehicle_model]);
    
INI_Int(File"Price"VehicleStatistics[vehicleid][vehicle_price]);
    
INI_Float(File"PosX"VehicleStatistics[vehicleid][vehicle_position][1]);
    
INI_Float(File"PosY"VehicleStatistics[vehicleid][vehicle_position][2]);
    
INI_Float(File"PosZ"VehicleStatistics[vehicleid][vehicle_position][3]);
    
INI_Float(File"PosA"VehicleStatistics[vehicleid][vehicle_position][4]);
    
INI_Int(File"OnSale"VehicleStatistics[vehicleid][vehicle_onsale]);
    return 
1;
}
stock SaveDealershipCars()
{
    new 
filestring[64];
    
    for(new 
0idx = -1MAX_DVEHICLESi++) // Loop through the cars
    
{
        if(
VehicleStatistics[i][vehicle_model] < 400) continue; // Checking if the slot isn't taken (if it isn't, 'continue' to the next iteration), you can use another variable if you wish
        
idx++;
        
        
format(filestringsizeof(filestring), "Dealership/Car_%d.ini"idx); 
        new 
INI:File INI_Open(filestring); // Open the file
        
INI_SetTag(File,"data");
        
        
INI_WriteInt(File"Model"VehicleStatistics[i][vehicle_model]);
        
INI_WriteInt(File"Price"VehicleStatistics[i][vehicle_price]);
        
INI_WriteFloat(File"PosX"VehicleStatistics[i][vehicle_position][1]);
        
INI_WriteFloat(File"PosY"VehicleStatistics[i][vehicle_position][2]);
        
INI_WriteFloat(File"PosZ"VehicleStatistics[i][vehicle_position][3]);
        
INI_WriteFloat(File"PosA"VehicleStatistics[i][vehicle_position][4]);
        
INI_WriteInt(File"OnSale"VehicleStatistics[i][vehicle_onsale]);
        
        
INI_Close(File); // Close the files
    
}
    print(
"Dealership cars saved successfully.");
    return 
1;

Be aware though, I haven't tested this. If you get any errors, please post them.

I just realized your location says DEFQON.1... Hardstyle for life mate!
Reply
#7

Well tbh I did not want to use YSI on my gamemode because I will have to change many other things, can it work the same version I had it? If so can you show me how? Thanks and +rep if you do.
Reply
#8

Quote:
Originally Posted by Adam_Hardy
Посмотреть сообщение
Well tbh I did not want to use YSI on my gamemode because I will have to change many other things, can it work the same version I had it? If so can you show me how? Thanks and +rep if you do.
You can tell me anything you need to change, and I'll help you out with it.
Reply
#9

Oh dang I just noticed the Hardstyle reply, thank you lol I'm in dead love with Hardstyle for 10 years xD Welcome back Heady <3

You didn't define File in the public function.
Reply
#10

Quote:
Originally Posted by Adam_Hardy
Посмотреть сообщение
Oh dang I just noticed the Hardstyle reply, thank you lol I'm in dead love with Hardstyle for 10 years xD Welcome back Heady <3

You didn't define File in the public function.
My bad. The 'File' shouldn't even be there when reading a file.

PHP код:
forward LoadVehicle_data(vehicleid,name[],value[]); 
public 
LoadVehicle_data(vehicleid,name[],value[]) 

    
INI_Int("Model"VehicleStatistics[vehicleid][vehicle_model]); 
    
INI_Int("Price"VehicleStatistics[vehicleid][vehicle_price]); 
    
INI_Float("PosX"VehicleStatistics[vehicleid][vehicle_position][1]); 
    
INI_Float("PosY"VehicleStatistics[vehicleid][vehicle_position][2]); 
    
INI_Float("PosZ"VehicleStatistics[vehicleid][vehicle_position][3]); 
    
INI_Float("PosA"VehicleStatistics[vehicleid][vehicle_position][4]); 
    
INI_Int("OnSale"VehicleStatistics[vehicleid][vehicle_onsale]); 
    return 
1

Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)