Need Help With FRWRITE
#1

Hello i need help with fwrite , i am not used it but i tryid to use it , i wanna save the cp defines per row to the ini file..

my script:
if someone have a suggestions how to fix this , the cmd writes only one line , and no more.. just rewriting the one line.
pawn Код:
dcmd_cp(playerid, params[])
{
    new reason[128]; new string[256];

    if (sscanf(params, "z", reason)) return SendClientMessage(playerid,0xFFFFFF60, "Kasuta: /cp <nimi>");
   
    if (GetPlayerState(playerid) == 2)
    {
        new stringid[2012]; // Oo jaa..
        new stringid2[2012]; // Maailmalхpp
       
        new Float:XP,Float:YP,Float:ZP;
        new RedNeckiAuto = GetPlayerVehicleID(playerid);

        GetVehiclePos(RedNeckiAuto,XP,YP,ZP);

        new File:fhnd;
       
       
        format(stringid2,2012,"%s",fread(fhnd, stringid2, -1));
       
        format(stringid,2012,"%s\r\n#define %s %0.3f,%0.3f,%0.3f\r\n",stringid2,reason,XP,YP,ZP);

        format(string, 300, "Checkpoint: {FFF45F}%s {FFFFFF}On Salvestatud! X: {FFF45F}%0.3f {FFFFFF}| Y: {FFF45F}%0.3f {FFFFFF}| Z: {FFF45F}%0.3f", reason,XP,YP,ZP);
        SendClientMessage(playerid, 0xFFFFFF60, string);

        SetPlayerRaceCheckpoint(playerid, 2, XP,YP,ZP, XP,YP,ZP, 15.0);
       
       
        format(string, 256, "Checkpointid.ini");
        fhnd=fopen(string ,io_write);
        fwrite(fhnd, stringid);
        fwrite(fhnd, "\n");
        fclose(fhnd);


    }
   
    return 1;
}
Reply
#2

It isn't fwrite which is causing the problem. It's the mode you open the file with; with fopen.

io_write - (what you're using) opens the file from offset 0, i.e. the start of the file. This means that whenever you write with fwrite after opening it this way, it'll write from the start and delete any other text.

io_append - is what you're looking for. This opens the file at the end, so when you write with fwrite, it'll append text to the end of the file - not deleting anything else.

To sum up, this adds text to the beginning, because of io_write:

pawn Код:
//New file var
new File:fpFile;

//Open for writing
fpFile=fopen("myFile.txt" ,io_write);

//Write "hi" at position 0
fwrite(fpFile, "hi");

//Close the file.
fclose(fpFile);
and this adds text to the end of the file, because of io_append:
pawn Код:
//New file var
new File:fpFile;

//Open for appending
fpFile=fopen("myFile.txt" ,io_append);

//Append "hi"
fwrite(fpFile, "hi");

//New line
fwrite(fpFile, "\r\n");

//Close the file.
fclose(fpFile);
Anyways, here's the (hopefully) fixed code:

pawn Код:
dcmd_cp(playerid, params[])
{
    new reason[128]; new string[256];

    if (sscanf(params, "z", reason)) return SendClientMessage(playerid,0xFFFFFF60, "Kasuta: /cp <nimi>");
   
    if (GetPlayerState(playerid) == 2)
    {
        new stringid[2012]; // Oo jaa..
        new stringid2[2012]; // Maailmalхpp
       
        new Float:XP,Float:YP,Float:ZP;
        new RedNeckiAuto = GetPlayerVehicleID(playerid);

        GetVehiclePos(RedNeckiAuto,XP,YP,ZP);

        new File:fhnd;
       
       
        format(stringid2,2012,"%s",fread(fhnd, stringid2, -1));
       
        format(stringid,2012,"%s\r\n#define %s %0.3f,%0.3f,%0.3f\r\n",stringid2,reason,XP,YP,ZP);

        format(string, 300, "Checkpoint: {FFF45F}%s {FFFFFF}On Salvestatud! X: {FFF45F}%0.3f {FFFFFF}| Y: {FFF45F}%0.3f {FFFFFF}| Z: {FFF45F}%0.3f", reason,XP,YP,ZP);
        SendClientMessage(playerid, 0xFFFFFF60, string);

        SetPlayerRaceCheckpoint(playerid, 2, XP,YP,ZP, XP,YP,ZP, 15.0);
       
       
        format(string, 256, "Checkpointid.ini");
        fhnd=fopen(string ,io_append);
        fwrite(fhnd, stringid);
        fwrite(fhnd, "\n");
        fclose(fhnd);


    }
   
    return 1;
}
I'd also advise turning down the size of those character arrays you have there, 2012 cells? o_O
Reply
#3

Quote:
Originally Posted by blewert
Посмотреть сообщение
It isn't fwrite which is causing the problem. It's the mode you open the file with; with fopen.

io_write - (what you're using) opens the file from offset 0, i.e. the start of the file. This means that whenever you write with fwrite after opening it this way, it'll write from the start and delete any other text.

io_append - is what you're looking for. This opens the file at the end, so when you write with fwrite, it'll append text to the end of the file - not deleting anything else.

To sum up, this adds text to the beginning, because of io_write:

pawn Код:
//New file var
new File:fpFile;

//Open for writing
fpFile=fopen("myFile.txt" ,io_write);

//Write "hi" at position 0
fwrite(fpFile, "hi");

//Close the file.
fclose(fhnd);
and this adds text to the end of the file, because of io_append:
pawn Код:
//New file var
new File:fpFile;

//Open for appending
fpFile=fopen("myFile.txt" ,io_append);

//Append "hi"
fwrite(fpFile, "hi");

//New line
fwrite(fpFile, "\r\n");

//Close the file.
fclose(fhnd);
Anyways, here's the (hopefully) fixed code:

pawn Код:
dcmd_cp(playerid, params[])
{
    new reason[128]; new string[256];

    if (sscanf(params, "z", reason)) return SendClientMessage(playerid,0xFFFFFF60, "Kasuta: /cp <nimi>");
   
    if (GetPlayerState(playerid) == 2)
    {
        new stringid[2012]; // Oo jaa..
        new stringid2[2012]; // Maailmalхpp
       
        new Float:XP,Float:YP,Float:ZP;
        new RedNeckiAuto = GetPlayerVehicleID(playerid);

        GetVehiclePos(RedNeckiAuto,XP,YP,ZP);

        new File:fhnd;
       
       
        format(stringid2,2012,"%s",fread(fhnd, stringid2, -1));
       
        format(stringid,2012,"%s\r\n#define %s %0.3f,%0.3f,%0.3f\r\n",stringid2,reason,XP,YP,ZP);

        format(string, 300, "Checkpoint: {FFF45F}%s {FFFFFF}On Salvestatud! X: {FFF45F}%0.3f {FFFFFF}| Y: {FFF45F}%0.3f {FFFFFF}| Z: {FFF45F}%0.3f", reason,XP,YP,ZP);
        SendClientMessage(playerid, 0xFFFFFF60, string);

        SetPlayerRaceCheckpoint(playerid, 2, XP,YP,ZP, XP,YP,ZP, 15.0);
       
       
        format(string, 256, "Checkpointid.ini");
        fhnd=fopen(string ,io_append);
        fwrite(fhnd, stringid);
        fwrite(fhnd, "\n");
        fclose(fhnd);


    }
   
    return 1;
}
I'd also advise turning down the size of those character arrays you have there, 2012 cells? o_O
yeah 2012 XD im just wondering of limit... anyways thanks! now i know that i know rep+
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)