Author Topic: [SOLVED] Reading a file  (Read 958 times)

I understand how to write to a file and how to create files but the problem is how to read a file.
Code: [Select]
function serverCmdReadLog(%client)
{
%file.openForAppend("config/server/IPLogger/logs.log");
        %file.readLine();
$Contents = %file.readLine();
messageClient(%client,'',$Contents);
}
It doesn't look right and it has no syntax errors. Possibly I am doing it wrong? If so, please help.
« Last Edit: December 10, 2011, 09:32:58 PM by FFSO »

openForAppend is for writing, use openForRead
Also, that will only message the client the first line. If you want to message them all lines, do this:
Code: [Select]
while(!%file.isEoF())
{
    messageClient(%client,'','%file.readLine());
}
You should also close and delete the objects
Code: [Select]
%file.close();
%file.delete();

Ok so...
Code: [Select]
function serverCmdReadLog(%client)
{
%file.openForRead("config/server/IPLogger/logs.log");
        %file.readLine();
while(!%file.isEoF())
{
messageClient(%client,'',%file.readLine());
}
%file.close();
%file.delete();
}

EDIT: your code HeadCrab Zombie had an unnecessary ' on line 3 causing a syntax error
« Last Edit: December 10, 2011, 03:03:15 PM by FFSO »

Get rid of the first %file.readLine();

EDIT: your code HeadCrab Zombie had an unnecessary ' on line 3 causing a syntax error
My code is lines 5-10. Line 3 is your own code, and I don't see anything wrong with it.

messageClient(%client,'','%file.readLine());

Current:
Code: [Select]
function serverCmdReadLog(%client)
{
%file.openForRead("config/server/IPLogger/logs.log");
while(!%file.isEoF())
{
messageClient(%client,'',%file.readLine());
}
%file.close();
%file.delete();
}

Oh, I was looking at the complete function you recently posted

That should work unless I overlooked something else

EDIT: Doesn't work console cant identify 'isEoF' and it re says the error over and over and crashes the game :|
« Last Edit: December 10, 2011, 03:19:10 PM by FFSO »

added %file = new FileObject();

Current:
Code: [Select]
function serverCmdReadLog(%client)
{
%file = new FileObject();
%file.openForRead("config/server/IPLogger/logs.log");
while(!%file.isEoF())
{
messageClient(%client,'',%file.readLine());
}
%file.close();
%file.delete();
}

Code: [Select]
function serverCmdReadLog(%client)
{
if(!isFile("config/server/IPLogger/logs.log"))
{
messageClient(%client, '', "\c6Log file does not exist or wrong file specified.");
return 0;
}
%file = new FileObject();
%file.openForRead("config/server/IPLogger/logs.log");
while(!%file.isEOF())
{
%line = %file.readLine();
messageClient(%client, '', %line);
}
%file.close();
%file.delete();
}
Try that. I modified a working file reader that I made (that again, works 100% for me), if this doesnt work, then you have some sort of script or addon that is loving up your blockland.

EDIT: the code worked but apparently it is saying that the file doesn't exist even though it does... the paths are all correct...Console is saying autoAdminCheck isn't a valid command (I used it to write to the file on someone joining...)

EDIT 2: Fixed
« Last Edit: December 10, 2011, 09:32:48 PM by FFSO »