Someone please explain me this?
#1

Hi,
how this two codes work?
Код:
LoadDealerships()
{
	new File:handle, count;
	new filename[64], line[256], s, key[64];
	for(new i=1; i < MAX_DEALERSHIPS; i++)
	{
		format(filename, sizeof(filename), DEALERSHIP_FILE_PATH "d%d.ini", i);
		if(!fexist(filename)) continue;
		handle = fopen(filename, io_read);
		while(fread(handle, line))
		{
			StripNL(line);
			s = strfind(line, "=");
			if(!line[0] || s < 1) continue;
			strmid(key, line, 0, s++);
			if(strcmp(key, "Created") == 0) DealershipCreated[i] = strval(line[s]);
			else if(strcmp(key, "Pos") == 0) sscanf(line[s], "p,fff", DealershipPos[i][0],
				DealershipPos[i][1], DealershipPos[i][2]);
		}
		fclose(handle);
		if(DealershipCreated[i]) count++;
	}
	printf("  Loaded %d dealerships", count);
}
Код:
SaveDealership(dealerid)
{
	new filename[64], line[256];
	format(filename, sizeof(filename), DEALERSHIP_FILE_PATH "d%d.ini", dealerid);
	new File:handle = fopen(filename, io_write);
	format(line, sizeof(line), "Created=%d\r\n", DealershipCreated[dealerid]); fwrite(handle, line);
	format(line, sizeof(line), "Pos=%.3f,%.3f,%.3f\r\n", DealershipPos[dealerid][0],
		DealershipPos[dealerid][1], DealershipPos[dealerid][2]);
	fwrite(handle, line);
	fclose(handle);
}
Reply
#2

PHP код:
LoadDealerships() 
{
    new 
File:handlecount// creating variable file and count for later use
    
new filename[64], line[256], skey[64]; // creating strings filename, line, variable s, and key for later use
    
for(new i=1MAX_DEALERSHIPSi++) // looping through MAX_DEALERSHIPS. MAX_DEALERSHIPS would be defined at the top of the script. And will be in numbers.
    
{
        
format(filenamesizeof(filename), DEALERSHIP_FILE_PATH "d%d.ini"i); // Formating the filename with the current value of i of the loop. DEALERSHIP_FILE_PATH is the path defined on top of your script.
        
if(!fexist(filename)) continue; // If file doesnt exists go to end of the loop.
        
handle fopen(filenameio_read); // Opens the file if exists and reads the info from it.
        
while(fread(handleline)) // Loops through the file.
        
{
            
StripNL(line);
            
strfind(line"="); // Checking if there's '=' sign in the line.
            
if(!line[0] || 1) continue; // If there isnt any line go to end of the loop.
            
strmid(keyline0s++); //Extracting the first character of the line.
            
if(strcmp(key"Created") == 0DealershipCreated[i] = strval(line[s]); // If string key has value "Created" then it starts this function.
            
else if(strcmp(key"Pos") == 0sscanf(line[s], "p,fff"DealershipPos[i][0], // Else if it has value "Pos" then it does that sscanf function.
                
DealershipPos[i][1], DealershipPos[i][2]);
        }
        
fclose(handle); // Closing the file.
        
if(DealershipCreated[i]) count++; // If the dealership is successfully created, then increase the value of count.
    
}
    
printf("  Loaded %d dealerships"count); // prints the number of loaded dealerships using the count variable.
}
SaveDealership(dealerid)
{
    new 
filename[64], line[256]; // creating strings for later use.
    
format(filenamesizeof(filename), DEALERSHIP_FILE_PATH "d%d.ini"dealerid); // formatting the filename string with the path and the id.
    
new File:handle fopen(filenameio_write); // Opening the file using the string filename which was formatted.
    
format(linesizeof(line), "Created=%d\r\n"DealershipCreated[dealerid]); fwrite(handleline); // Formatting the line string with the value that if the dealership is created or not. /r = start from beginning of the line. \n = new line.
    
format(linesizeof(line), "Pos=%.3f,%.3f,%.3f\r\n"DealershipPos[dealerid][0], // Formatting the line with Position of the dealership.
        
DealershipPos[dealerid][1], DealershipPos[dealerid][2]); 
    
fwrite(handleline); // Writing the file.
    
fclose(handle); // Closing the file.

Reply
#3

Code is being read from up to down and left to right usually. Not sure if code by arabic standards exists.
Reply
#4

Quote:
Originally Posted by Sunehildeep
Посмотреть сообщение
PHP код:
LoadDealerships() 
{
    new 
File:handlecount// creating variable file and count for later use
    
new filename[64], line[256], skey[64]; // creating strings filename, line, variable s, and key for later use
    
for(new i=1MAX_DEALERSHIPSi++) // looping through MAX_DEALERSHIPS. MAX_DEALERSHIPS would be defined at the top of the script. And will be in numbers.
    
{
        
format(filenamesizeof(filename), DEALERSHIP_FILE_PATH "d%d.ini"i); // Formating the filename with the current value of i of the loop. DEALERSHIP_FILE_PATH is the path defined on top of your script.
        
if(!fexist(filename)) continue; // If file doesnt exists go to end of the loop.
        
handle fopen(filenameio_read); // Opens the file if exists and reads the info from it.
        
while(fread(handleline)) // Loops through the file.
        
{
            
StripNL(line);
            
strfind(line"="); // Checking if there's '=' sign in the line.
            
if(!line[0] || 1) continue; // If there isnt any line go to end of the loop.
            
strmid(keyline0s++); //Extracting the first character of the line.
            
if(strcmp(key"Created") == 0DealershipCreated[i] = strval(line[s]); // If string key has value "Created" then it starts this function.
            
else if(strcmp(key"Pos") == 0sscanf(line[s], "p,fff"DealershipPos[i][0], // Else if it has value "Pos" then it does that sscanf function.
                
DealershipPos[i][1], DealershipPos[i][2]);
        }
        
fclose(handle); // Closing the file.
        
if(DealershipCreated[i]) count++; // If the dealership is successfully created, then increase the value of count.
    
}
    
printf("  Loaded %d dealerships"count); // prints the number of loaded dealerships using the count variable.
}
SaveDealership(dealerid)
{
    new 
filename[64], line[256]; // creating strings for later use.
    
format(filenamesizeof(filename), DEALERSHIP_FILE_PATH "d%d.ini"dealerid); // formatting the filename string with the path and the id.
    
new File:handle fopen(filenameio_write); // Opening the file using the string filename which was formatted.
    
format(linesizeof(line), "Created=%d\r\n"DealershipCreated[dealerid]); fwrite(handleline); // Formatting the line string with the value that if the dealership is created or not. /r = start from beginning of the line. \n = new line.
    
format(linesizeof(line), "Pos=%.3f,%.3f,%.3f\r\n"DealershipPos[dealerid][0], // Formatting the line with Position of the dealership.
        
DealershipPos[dealerid][1], DealershipPos[dealerid][2]); 
    
fwrite(handleline); // Writing the file.
    
fclose(handle); // Closing the file.

Thanks dude!
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)