Author Topic: Files  (Read 2195 times)

What are the commands for manipulating files?

1. create a file object
2. do crap

Code: [Select]
%file = new FileObject();
%file.openForAppend("./omgwtfbbq.txt");
%file.writeLine("line\r\n");
%file.close();
%file.delete();
Include the \r\n to make a new line.


Checkout the LogWriter for some more file stuff
http://blockland.kalphiter.com/add-ons/download.php?filename=Server_LogWriter

For writing to or reading from a file you'll need a fileObject.

Reading:
Code: [Select]
%fo = new fileObject();
%fo.openForRead("file.txt");
while(!%fo.isEOF()) //while we are not at the end of the file
{
    echo(%fo.readLine());
}
%fo.close();
%fo.delete();

You can use %fo.writeLine("text"); after %fo.openForWrite(%file); or %fo.openForAppend(%file); (starts writing at the end of the file instead of clearing the file first).

Additionally there is the fileDelete(%file); function, and you don't need a fileObject for it. I'll let you figure out what that does.

Code: [Select]
%f = new FileObject(); %f.dump();
Or, just a class dump:
Code: [Select]
class  FileObject : public SimObject {
  public:
   virtual bool openForRead(string filename) {}
   virtual bool openForWrite(string filename) {}
   virtual bool openForAppend(string filename) {}
   /*! Are we at the end of the file? */
   virtual bool isEOF() {}
   /*! Read a line from the file. */
   virtual string readLine() {}
   /*! Write a line to the file, if it was opened for writing. */
   virtual void writeLine(string text) {}
   /*! Close the file. */
   virtual void close() {}
};

I obviously did this wrong... What did I do wrong?

Code: [Select]
function servercmdnote(%client, %note1, %note2, %note3, %note4, %note5, %note6, %note7, %note8, %note9, %note10, %note11, %note12, %note13, %note14, %note15, %note16, %note17, %note18, %note19, %note20, %note21, %note22)
{
%note = %note1 SPC %note2 SPC %note3 SPC %note4 SPC %note5 SPC %note6 SPC %note7 SPC %note8 SPC %note9 SPC %note10 SPC %note11 SPC %note12 SPC %note13 SPC %note14 SPC %note15 SPC %note16 SPC %note17 SPC %note18 SPC %note19 SPC %note20 SPC %note21 SPC %note22;
%notes = new fileobject();
%notes.openforappend("./Servernotes.txt");
%notes.writeLine(%note);
}

function servercmdnotes(%client)
{
while(!%notes.isEOF())
{
%noteread = %notes.readline();
messageclient(%client, '', %noteread);
}
}
« Last Edit: November 16, 2009, 08:30:37 PM by Deriboy »

I don't think you can call a "./filePath" in a fileObject, but I'm not sure about that one.
You should close and delete the fileObject after you use it.
Each function you need to re-open the fileObject and open it for read/append/write.

I think it might be that there is no Servernotes.txt file to append to, try creating one or making the path absolute (i.e. "Add-Ons/Mod_Name/...") like Lilboarder suggested. And like he said, you will need to recreate and delete the file object within both functions or it won't work.

new fileobject();

When the object is created:
fileobject.openforwrite(filename); - Opens the file (with path included) to overwrite or create.
fileobject.openforread(filename);  - Opens the file (bla bla) to read.
fileobject.openforappend(filename);  - Opens the file (lol) to put stuff at the end of it. Not sure if this creates a file.

When the object is writing/appending:
fileobject.writeline(line);  - Adds a line to the end of the file. Remember openforwrite already cleared the file.

When the object is reading:
fileobject.readline(); - Returns the next line after the one that's been read, or the first line if this is first time used.
fileobject.isEOF();  - Returns 1 if there are no more lines to read.

When the object is reading/writing/appending:
fileobject.close();  - save the file if writing/appending, and allow other programs to access the file.


fileobject.delete(); - deletes the object, use this when the object is no longer needed.

Functions that don't need an object:
fileDelete(filename); - Deletes the file (filepath included).
discoverFile(filename);  - Allows changed files in the zip to be read if file is a zip. Allows file to be used if new.
findFirstFile(filequery);  - Returns the filename(with path) of the first file that meets the query. (wildcard * is able to be used)
findNextFile(filequery);  - Finds the next file that meets the same query.
isFile(filename);  - returns 1 if the file exists. If the file exists but still returns 0, it is a new file and you need to use discoverFile.

Thanks. Also, how do you kick or ban a player?

Through script/console?

kickBLID(id);
banBLID(id,time,raisinreason);

It will say that the CONSOLE has kicked/banned them though.

Is there any way to prevent that?

No. You would need to make your own functions.
Not sure how banning would work, however kicking would just find the client and delete them.

GameConnection::delete(string reason)

k cool we know you know how functions are named and such, now can you put it in a way that those who are far behind your knowledge can understand?

Also: How to use schedules?