SA-MP Forums Archive
How to do this ? - 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: How to do this ? (/showthread.php?tid=478157)



Solved : Code / Server Stops when write - Joe_Goro - 28.11.2013

Before reading the problem solved by : https://github.com/Zeex/samp-plugin-...tect/issues/25 check this link .. thanks for all and Crash-detect team
hello i have this code
pawn Код:
new File:fp = fopen("sucks.txt", io_read);
if (fp) {
    new buf[128];
    fread(fp, buf);
    fclose(fp);
} else {
    printf("failed to open sucks.txt");
}
now how to make the script create the file if its not exist


Re: How to do this ? - xVIP3Rx - 28.11.2013

Check if it exist before reading it, And if not, then create it, using Fexist
pawn Код:
if(!fexist("sucks.txt"))
{
    printf("failed to open sucks.txt, creating new one..");
       
    new File:file = fopen("sucks.txt", io_write);
    if(file) fclose(file);
}
new File:fp = fopen("sucks.txt", io_read);
if (fp)
{
    new buf[128];
    fread(fp, buf);
    fclose(fp);
}



Re: How to do this ? - Konstantinos - 28.11.2013

https://sampwiki.blast.hk/wiki/Fopen

Quote:

io_read Reads from the file.
io_write Write in the file, or create the file. Erases all contents.
io_readwrite Reads the file or creates it
io_append Appends (adds) to file, write-only. If the file does not exist, it is created
So I guess you'll have to open the file and then close it, so it'll be created.

pawn Код:
if( !fexist( "sucks.txt" ) )
{
    new
        File: file = fopen( "sucks.txt", io_write )
    ;
    fclose( file );
}



Re: How to do this ? - Joe_Goro - 28.11.2013

is there is something wrong with this code ?
all my script have the same method but my server crash because he cant write on non exsit file
is there is any way to check what is the non exist file ?
pawn Код:
stock CheckBan(ip[])
{
    new string[20];
    if(fexist("ban.cfg"))
    {
        new File: file = fopen("ban.cfg", io_read);
        while(fread(file, string))
        {
            if (strcmp(ip, string, true, strlen(ip)) == 0)
            {
                fclose(file);
                return 1;
            }
        }
        fclose(file);
    }
    return 0;
}



Re: How to do this ? - Konstantinos - 28.11.2013

https://sampwiki.blast.hk/wiki/Fclose

Quote:
Important Note: The file handle must be valid, and must point to a file successfully opened by fopen.

Change to:
pawn Код:
if(file) fclose(file);



Re: How to do this ? - Joe_Goro - 28.11.2013

hmm i will try it and reply
btw server cant write on non-exist file not close it as what i see
Код:
#./samp03svr
Started server on port: 7775, with maxplayers: 70 lanmode is OFF.


samp03svr: amx/amxfile.c:101: fgets_cell: Assertion `fp!=((void *)0)' failed.
Aborted
#
iam right ? the server cant write on file ?


Re: How to do this ? - Konstantinos - 28.11.2013

Make sure that scriptfiles directory's permissions are set to writable/readable.


Re: How to do this ? - Joe_Goro - 28.11.2013

by ""chmod 777 scriptfiles"" ? yes is it


Re: How to do this ? - Joe_Goro - 28.11.2013

Thanks for all
SOLVED by : https://github.com/Zeex/samp-plugin-...tect/issues/25