Blockland Forums > Modification Help

[SOLVED] Reading a file

Pages: (1/2) > >>

FFSO:

I understand how to write to a file and how to create files but the problem is how to read a file.

--- Code: ---function serverCmdReadLog(%client)
{
%file.openForAppend("config/server/IPLogger/logs.log");
        %file.readLine();
$Contents = %file.readLine();
messageClient(%client,'',$Contents);
}

--- End code ---
It doesn't look right and it has no syntax errors. Possibly I am doing it wrong? If so, please help.

Headcrab Zombie:

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: ---while(!%file.isEoF())
{
    messageClient(%client,'','%file.readLine());
}
--- End code ---
You should also close and delete the objects

--- Code: ---%file.close();
%file.delete();

--- End code ---


FFSO:

Ok so...

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

--- End code ---

EDIT: your code HeadCrab Zombie had an unnecessary ' on line 3 causing a syntax error

Headcrab Zombie:

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


--- Quote from: FFSO on December 10, 2011, 04:01:20 PM ---EDIT: your code HeadCrab Zombie had an unnecessary ' on line 3 causing a syntax error

--- End quote ---
My code is lines 5-10. Line 3 is your own code, and I don't see anything wrong with it.

FFSO:

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

Current:

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


Pages: (1/2) > >>

Go to full version