Author Topic: [Guide] Reading/Writing/Creating a file  (Read 1795 times)

Since the old guide is very hard to find I decided to make a new one.



Subject 1: Reading a file

Code: [Select]
function serverCmdRead(%client, %filePath)
{
    if(!isFile(%filePath)) //FilePath must be config/server/YourFolder/YourFile.txt or .log
    {
        messageClient(%client, '', "\c2"@ %filePath @"\c6 doesn't exist. Most possible conclusion: unknown file extension/file path.");
        return 0;
    }
    %file = new FileObject();
    %file.openForRead(%filePath);
    while(!%file.isEOF())
    {
        %line = %file.readLine();
        messageClient(%client, '', %line);
    }
    %file.close();
    %file.delete();
}



Subject 2: Writing to a file

Code: [Select]
$File::location = "wherever you want it to be, must be in config/server/";
$File::file = "yourlocation, config/server/YourFile/file.txt or .log, doesn't matter.";

//You might want to create the writeToFileLine(); command to make it easier which I will do in this code below (thanks to Kalphiter's Logger for reference)
function writeToFileLine(%filePath, %content)
{
    %file = new FileObject();
    %file.openForAppend(%filePath);
    %file.writeLine(%content);
    %file.close();
    %file.delete();
}
//Now for the code to write to the file (I will be using the autoAdminCheck as a writing subject, for logging an IP address( you can change the code for any other things you might want it to be used for))

function GameConnection::autoAdminCheck(%client)
{
    Parent::autoAdminCheck(%client);

    writeToFileLine($File::file, %client.name TAB %client.bl_id TAB %client.getRawIP()); //Used from Kalphiter's IP Logger
}



Subject 3: Creating a file

HINT: You do not have to CREATE a file manually if you use the AutoAdminCheck command with the $File::location and $File::file variable, since the file will be automatically created to your specifications and locations.



If you have any confusion with my demonstration or if I had a syntax error somewhere (unlikely) or even if you have a version that might be better, please describe your idea or what I did wrong with the updated piece of code.
« Last Edit: December 30, 2011, 04:34:20 PM by FFSO »


Good tutorial, useful references in there.
But I think you should add an explanation of the basic functions: .readLine .writeLine .close .openFor* e.t.c.

Good tutorial, useful references in there.
But I think you should add an explanation of the basic functions: .readLine .writeLine .close .openFor* e.t.c.

They are pretty obvious but I will add them in a bit anyway since a lot of coders do not know the real meaning of them.