SA-MP Forums Archive
fwrite(file [Add 1]?? - Printable Version

+- SA-MP Forums Archive (https://sampforum.blast.hk)
+-- Forum: SA-MP Scripting and Plugins (https://sampforum.blast.hk/forumdisplay.php?fid=8)
+--- Forum: Scripting Help (https://sampforum.blast.hk/forumdisplay.php?fid=12)
+--- Thread: fwrite(file [Add 1]?? (/showthread.php?tid=568508)



fwrite(file [Add 1]?? - kevannkw - 22.03.2015

So i wonder if i can add 1 with "fwrite"?

So let's say i have 0 in the file and i want to add 1, it increases to 1, then if i do the same command again it should add on the 1 that already exist so it makes it 2?


AW: fwrite(file [Add 1]?? - Sascha - 22.03.2015

either use some dini system or whatever or something like that:
pawn Код:
new tmp[10];
new File:myfile = fopen("yourfile.txt", io_readwrite);
fread(myfile, tmp);
format(tmp, sizeof(tmp), "%d", strval(tmp) + 1);
fwrite(myfile, tmp);
fclose(myfile);
although this might be the least efficient way


Re: fwrite(file [Add 1]?? - BleverCastard - 22.03.2015

Deleted.


Re: fwrite(file [Add 1]?? - kevannkw - 22.03.2015

Quote:
Originally Posted by BleverCastard
Посмотреть сообщение
Deleted.
What do you mean "Deleted"?


AW: Re: fwrite(file [Add 1]?? - Sascha - 22.03.2015

Quote:
Originally Posted by kevannkw
Посмотреть сообщение
What do you mean "Deleted"?
he wrote some message there and might have noticed that what he wrote was wrong or whatever, therefor he replaced it with "deleted" with what he means "just ignore this"


Re: fwrite(file [Add 1]?? - Abagail - 22.03.2015

You have to open the file as read-only, read the contents, close the file, open the file as write-only, write the contents and close the file.

pawn Код:
stock AddOneToFile(file[])
{
     new File:handle = fopen(file, io_read), buf[6];

if(handle)
{
    fread(handle, buf);
    number = strval(buf);
    fclose(handle);
    new File:handle = fopen(file, io_write);

    if(handle)
    {
        fwrite(handle, number+1);
       
        fclose(handle);
    }
}

return 1;
}



Re: fwrite(file [Add 1]?? - kevannkw - 22.03.2015

Quote:
Originally Posted by ******
Посмотреть сообщение
Or you could save several stages by opening the file as readwrite.
Mind giving me an Example?


AW: fwrite(file [Add 1]?? - Sascha - 22.03.2015

Quote:
Originally Posted by Sascha
Посмотреть сообщение
either use some dini system or whatever or something like that:
pawn Код:
new tmp[10];
new File:myfile = fopen("yourfile.txt", io_readwrite);
fread(myfile, tmp);
format(tmp, sizeof(tmp), "%d", strval(tmp) + 1);
fwrite(myfile, tmp);
fclose(myfile);
although this might be the least efficient way
well I wrote this already