SA-MP Forums Archive
File writing - 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)
+---- Forum: Help Archive (https://sampforum.blast.hk/forumdisplay.php?fid=89)
+---- Thread: File writing (/showthread.php?tid=258599)



File writing - sim_sima - 31.05.2011

Hey guys. Can someone tell me why this doesnt work?
pawn Код:
public OnPlayerText(playerid, text[])
{
    new name[MAX_PLAYER_NAME], File:chatlog[128];
    GetPlayerName(playerid, name, sizeof(name));
    format(chatlog, sizeof(chatlog), "Logs/Chat/%s.txt", name);
    fopen(chatlog, io_readwrite);
    new chat[128], chater[128], day, month, year, hour, minute, second;
    GetPlayerName(playerid,chater,sizeof(chater));
    gettime(hour, minute, second);
    getdate(year, month, day);
    format(chat,sizeof(chat),"[%d/%d %d %d:%d:%d]: ( %s ): %s\n", day, month, year, hour, minute, second, chater, text);
    if(chatlog)
    {
        if(fexist(chatlog))
        {
            fwrite(chatlog,chat);
            fclose(chatlog);
        }
    }
    return 1;
}
Thank you.


Re: File writing - 0x5A656578 - 31.05.2011

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


Re: File writing - sim_sima - 31.05.2011

Thanks


Re: File writing - sim_sima - 31.05.2011

I dont know if you figured it out, but im trying to make a chat log, like when a player chats, it gets written in the file that has same name as the player .txt. And if that file doesnt exist, the server will create it.


Re: File writing - Sasino97 - 31.05.2011

Quote:
Originally Posted by sim_sima
Посмотреть сообщение
I dont know if you figured it out, but im trying to make a chat log, like when a player chats, it gets written in the file that has same name as the player .txt. And if that file doesnt exist, the server will create it.
pawn Код:
public OnPlayerText(playerid, text[])
{
  new string[128];
  new name[24];
  GetPlayerName(playerid, name, 24);
  new File:log = fopen(string, io_append);
  new hour, minute, second, year, month, day;
  gettime(hour, minute, second);
  getdate(year, month, day);
  format(string,sizeof(string), "[%d/%d/%d][%d:%d:%d]%s\r\n", day, month, year, hour, minute, second, text);
  fwrite(log, string);
  fclose(log);
  return 1;
}
EDIT: It crashed the server what's the error? (I tested this code now on rivershell by Kye)


Re: File writing - sim_sima - 31.05.2011

Quote:
Originally Posted by [GF]Sasino97
Посмотреть сообщение
pawn Код:
public OnPlayerText(playerid, text[])
{
  new string[128];
  new name[24];
  GetPlayerName(playerid, name, 24);
  new File:log = fopen(string, io_append);
  new hour, minute, second, year, month, day;
  gettime(hour, minute, second);
  getdate(year, month, day);
  format(string,sizeof(string), "[%d/%d/%d][%d:%d:%d]%s\r\n", day, month, year, hour, minute, second, text);
  fwrite(log, string);
  fclose(log);
  return 1;
}
But where do you define the file direction? Like "/Logs/Chat/%s.txt"?


Re: File writing - Sasino97 - 31.05.2011

New version:

pawn Код:
public OnPlayerText(playerid, text[])
{
  new string[128];
  new name[24];
  GetPlayerName(playerid, name, 24);
  format(string,sizeof(string), "Logs/Chat/%s.txt", name); // THIS WAS THE ERROR
  new File:log = fopen(string, io_append);
  new hour, minute, second, year, month, day;
  gettime(hour, minute, second);
  getdate(year, month, day);
  format(string,sizeof(string), "[%d/%d/%d][%d:%d:%d]%s\r\n", day, month, year, hour, minute, second, text);
  fwrite(log, string);
  fclose(log);
  return 1;
}



Re: File writing - sim_sima - 31.05.2011

I made this already:
pawn Код:
public OnPlayerText(playerid, text[])
{
    new name[MAX_PLAYER_NAME], file[128], chat[128], chater[MAX_PLAYER_NAME], year, month, day, hour, minute, second;
    GetPlayerName(playerid, name, sizeof(name));
    format(file, sizeof(file), "Logs/Chat/%s.txt", name);
    fopen(file, io_readwrite);
    GetPlayerName(playerid, chater, sizeof(chater));
    gettime(hour, minute, second);
    getdate(year, month, day);
    format(chat,sizeof(chat),"[%d/%d-%d %d:%d:%d]: ( %s ): %s\n", day, month, year, hour, minute, second, chater, text);
    if(file)
    {
        if(fexist(file))
        {
            fwrite(file,chat);
            fclose(file);
        }
    }
    return 1;
}
but it gives me this errors:
Код:
C:\Users\Simon\Desktop\samp server rpg\gamemodes\rpg-gm.pwn(609) : error 033: array must be indexed (variable "file")
C:\Users\Simon\Desktop\samp server rpg\gamemodes\rpg-gm.pwn(613) : error 035: argument type mismatch (argument 1)
C:\Users\Simon\Desktop\samp server rpg\gamemodes\rpg-gm.pwn(614) : error 035: argument type mismatch (argument 1)
Pawn compiler 3.2.3664	 	 	Copyright © 1997-2006, ITB CompuPhase


3 Errors.
Line 609, 613 and 614:
pawn Код:
if(file)
fwrite(file,chat);
fclose(file);
Thank you.


Re: File writing - sim_sima - 31.05.2011

Quote:
Originally Posted by [GF]Sasino97
Посмотреть сообщение
New version:

pawn Код:
public OnPlayerText(playerid, text[])
{
  new string[128];
  new name[24];
  GetPlayerName(playerid, name, 24);
  format(string,sizeof(string), "Logs/Chat/%s.txt", name); // THIS WAS THE ERROR
  new File:log = fopen(string, io_append);
  new hour, minute, second, year, month, day;
  gettime(hour, minute, second);
  getdate(year, month, day);
  format(string,sizeof(string), "[%d/%d/%d][%d:%d:%d]%s\r\n", day, month, year, hour, minute, second, text);
  fwrite(log, string);
  fclose(log);
  return 1;
}
Oh, ty


Re: File writing - Sasino97 - 31.05.2011

Quote:
Originally Posted by sim_sima
Посмотреть сообщение
I made this already:
pawn Код:
public OnPlayerText(playerid, text[])
{
    new name[MAX_PLAYER_NAME], file[128], chat[128], chater[MAX_PLAYER_NAME], year, month, day, hour, minute, second;
    GetPlayerName(playerid, name, sizeof(name));
    format(file, sizeof(file), "Logs/Chat/%s.txt", name);
    fopen(file, io_readwrite);
    GetPlayerName(playerid, chater, sizeof(chater));
    gettime(hour, minute, second);
    getdate(year, month, day);
    format(chat,sizeof(chat),"[%d/%d-%d %d:%d:%d]: ( %s ): %s\n", day, month, year, hour, minute, second, chater, text);
    if(file)
    {
        if(fexist(file))
        {
            fwrite(file,chat);
            fclose(file);
        }
    }
    return 1;
}
but it gives me this errors:
Код:
C:\Users\Simon\Desktop\samp server rpg\gamemodes\rpg-gm.pwn(609) : error 033: array must be indexed (variable "file")
C:\Users\Simon\Desktop\samp server rpg\gamemodes\rpg-gm.pwn(613) : error 035: argument type mismatch (argument 1)
C:\Users\Simon\Desktop\samp server rpg\gamemodes\rpg-gm.pwn(614) : error 035: argument type mismatch (argument 1)
Pawn compiler 3.2.3664	 	 	Copyright © 1997-2006, ITB CompuPhase


3 Errors.
Line 609, 613 and 614:
pawn Код:
if(file)
fwrite(file,chat);
fclose(file);
Thank you.
new file[128]
to
new File:file[128];

Quote:
Originally Posted by file.inc
native File:fopen(const name[], filemode: mode = io_readwrite);
File:fopen

Every native or stock with a tag returns a value with that tag, example:
pawn Код:
stock Mytag:ExampleStock();

//You can only do

new Mytag:a;

a = ExampleStock();

//and not

new a;

a = ExampleStock();