Little help overwriting a line of a file -
Mishima - 29.05.2012
Hi people.
I have a little system here to save the date of the last time a person entered a Gun shop.
Here's the code:
pawn Код:
public LastGunShopDate(playerid)
{
new str[128];
new File:enter;
new year,month,day;
getdate(year, month, day);
format(str, sizeof(str), "Gunshop:%d",P_Info[playerid][gunkey]);
fdeleteline("guns.cfg",str);
new sstr[128];
format(sstr, sizeof(sstr), "GunShop:%d - Date:%02d|%02d|%d\r\n",P_Info[playerid][gunkey],day,month,year);
enter=fopen("guns.cfg", io_append);//
fwrite(enter, sstr);
fclose(enter);
return 1;
}
Code to delete the line.
pawn Код:
stock fdeleteline(filename[], removed[])
{
new string[64], str[32], File:handle, File:ftmp;
handle = fopen(filename,io_read);
format(str,sizeof(str),"%s.pt",filename);
ftmp = fopen(str,io_write);
while(fread(handle,string))
if(strfind(string,removed) == -1)
fwrite(ftmp,string);
fclose(handle);
fclose(ftmp);
handle = fopen(filename,io_write);
ftmp = fopen(str,io_read);
while(fread(ftmp,string))
fwrite(handle,string);
fclose(handle);
fclose(ftmp);
return fremove(str);
}
On the command "/enter" and "/exit" I have the LastGunShopEntered(playerid);
Now Ingame, when I get in the Gunshop ID 2.
This is what happens in the file..
GunShop:0 - Date:10|03|2000
GunShop:1 - Date:10|03|2000
GunShop:3 - Date:10|03|2000
GunShop:4 - Date:10|03|2000
GunShop:5 - Date:10|03|2000
GunShop:2 - Date:29|05|2012
The order gets messed up, can somebody help with this? Please.
Re: Little help overwriting a line of a file -
Jonny5 - 29.05.2012
https://sampwiki.blast.hk/wiki/Fseek
try this, it will allow you to seek to a line before writing/reading from it
Re: Little help overwriting a line of a file -
Mishima - 29.05.2012
Can you give me a little example of how to use it?
I'm sorry, because I don't know very well how to.
Thanks for the reply by the way!
Re: Little help overwriting a line of a file -
Mishima - 30.05.2012
The only problem I have is the line being deleted and the new data always goes to the bottom of the file, but it should overwrite the line and not do this :S
I want this:
GunShop:0 - Date:10|03|2000
GunShop:1 - Date:10|03|2000
GunShop:2 - Date:29|05|2012
GunShop:3 - Date:10|03|2000
GunShop:4 - Date:10|03|2000
GunShop:5 - Date:10|03|2000
Not this:
GunShop:0 - Date:10|03|2000
GunShop:1 - Date:10|03|2000
GunShop:3 - Date:10|03|2000
GunShop:4 - Date:10|03|2000
GunShop:5 - Date:10|03|2000
GunShop:2 - Date:29|05|2012
Re: Little help overwriting a line of a file -
Jonny5 - 30.05.2012
i know fseek is the solution,
I have never used it so im not real sure,
the other option is to read all 6 shops into an array
edit the time on the current shop array item,
then save them all back into the file in correct order.
If i get some time after work ill try fseek as im sure its better than the array method.
regards,
Re: Little help overwriting a line of a file -
Mishima - 31.05.2012
I've tried putting it on a different way.
Like this:
pawn Код:
function LastHouseUpdates()
{
new x;
new File: file2;
while (x < sizeof(HSaveInfo))
{
new sstring[256];
format(sstring, sizeof(sstring), "GunShop:%d - Date:%02d|%02d|%d\n",HSaveInfo[x][sID],HSaveInfo[x][sDay],HSaveInfo[x][sMonth],HSaveInfo[x][sYear]);
if(x == 0)
{
file2 = fopen("guns.cfg", io_write);
}
else
{
file2 = fopen("guns.cfg", io_append);
}
fwrite(file2, sstring);
x++;
fclose(file2);
}
return 1;
}
I have this at the cmd /enter:
pawn Код:
getdate(HSaveInfo[i][sYear], HSaveInfo[i][sMonth], HSaveInfo[i][sDay]);
HSaveInfo[i][sID] = i;
LastHouseUpdates();
This all works, but there's only one problem. I need to make a way to load the file and the data that is saved in it. But I don't know how. Because at the moment the entered Gunshop is being saved correctly on its line, but the rest of the Gunshops are assuming zero. Every other line a 0;
Example, I entered the Gunshop id:2
And this happened:
Before:
GunShop:0 - Date:10|03|2000
GunShop:1 - Date:10|03|2000
GunShop:2 - Date:10|03|2000
GunShop:3 - Date:10|03|2000
GunShop:4 - Date:10|03|2000
GunShop:5 - Date:10|03|2000
After:
GunShop:0 - Date:00|00|00
GunShop:1 - Date:31|05|2012
GunShop:2 - Date:00|00|00
GunShop:3 - Date:00|00|00
GunShop:4 - Date:00|00|00
GunShop:5 - Date:00|00|00
Please help anybody. :\
Re: Little help overwriting a line of a file -
MadeMan - 31.05.2012
You can use this function
pawn Код:
stock freplaceline(filename[], find[], replace[])
{
if(!fexist(filename)) return 0;
new File:handle = fopen(filename, io_read);
if(!handle) return 0;
new File:tmp = ftemp();
if(!tmp)
{
fclose(handle);
return 0;
}
new line[256];
while(fread(handle, line))
{
if(strfind(line, find) == -1)
fwrite(tmp, line);
else
fwrite(tmp, replace);
}
fclose(handle);
fseek(tmp, 0);
handle = fopen(filename, io_write);
if(!handle)
{
fclose(tmp);
return 0;
}
while(fread(tmp, line))
{
fwrite(handle, line);
}
fclose(handle);
fclose(tmp);
return 1;
}
pawn Код:
public LastGunShopDate(playerid)
{
new str[128], sstr[128];
new year,month,day;
getdate(year, month, day);
format(str, sizeof(str), "Gunshop:%d",P_Info[playerid][gunkey]);
format(sstr, sizeof(sstr), "GunShop:%d - Date:%02d|%02d|%d\r\n",P_Info[playerid][gunkey],day,month,year);
freplaceline("guns.cfg",str,sstr);
return 1;
}
Re: Little help overwriting a line of a file -
Mishima - 31.05.2012
Thank you very much MadeMan, it worked, I appreciate it!!!