How to do this ?
#1

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
Reply
#2

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);
}
Reply
#3

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 );
}
Reply
#4

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;
}
Reply
#5

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);
Reply
#6

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 ?
Reply
#7

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

by ""chmod 777 scriptfiles"" ? yes is it
Reply
#9

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


Forum Jump:


Users browsing this thread: 1 Guest(s)