Author Topic: help with ReadFile  (Read 1398 times)

0 Members and 1 Guest are viewing this topic.

Offline frosty

  • Flagrunner
  • ****
  • Posts: 601
  • Uber Desert Eagle ^^
help with ReadFile
« on: February 17, 2012, 04:01:24 am »
seems i dont know how to use the search function, for the life of me i cannot find anything about reading a full file onto something like a writeconsole message

Code: (pascal) [Select]
Const
File_dir = 'Scripts/Adminnote/';
 CRLF = #13#10;

var
File_data:String;

procedure OnJoinTeam(ID,Team: byte);
begin
if Team = 1 then begin
File_data :=ReadFile(File_dir+'Note.txt');
File_data := copy(File_data, 0, length(File_data)-2);
WriteConsole(0,File_data,$EE81FAA1);
end;
end;

function OnCommand(ID: Byte; Text: string): boolean;
begin
if Text = '/adnote' then begin
File_data :=ReadFile(File_dir+'Note.txt');
File_data := copy(File_data, 0, length(File_data)-2);
WriteConsole(0,File_data,$EE81FAA1);
end;
end;

Note.txt attached

all its doing is showing a single blank writeconsole message ingame

all help appreciated

thanks in advance :)
check out my server! click here

If at first you don't succeed, Improvise! :D

Offline Falcon`

  • Flagrunner
  • ****
  • Posts: 792
  • A wanted lagger
Re: help with ReadFile
« Reply #1 on: February 17, 2012, 12:01:17 pm »
does this work on linux or windows?
If you're not paying for something, you're not the customer; you're the product being sold.
- Andrew Lewis

Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live.

Offline jrgp

  • Administrator
  • Flamebow Warrior
  • *****
  • Posts: 5037
Re: help with ReadFile
« Reply #2 on: February 17, 2012, 12:11:57 pm »
I don't know if scripts themselves are permitted to access the Scripts folder.
There are other worlds than these

Offline Tehbugerz0r

  • Soldier
  • **
  • Posts: 158
Re: help with ReadFile
« Reply #3 on: February 17, 2012, 01:09:22 pm »
Found the problem, while ReadFile is not case sensitive on windows servers it is on linux servers, so you need to make sure you have proper case, for example, change "Scripts" to "scripts" as that is the default name for it.

Weird, I know, but it works.

Offline tk

  • Soldier
  • **
  • Posts: 235
Re: help with ReadFile
« Reply #4 on: February 17, 2012, 02:29:38 pm »
It's HIGHLY recommended not to use ReadFile function in such events as OnJoinTeam, OnPlayerDamage, etc. It lowers stability. Also, each time someone joins/changes the team, your script will rape HDD. And what if it's currently busy? It will cause a lag spike then. Better load it once, at server's startup for example and save what you need.

Would be nice if you didn't hardcore thing such as a script name. It's better to set file directory as 'scripts' + ScriptName

Offline frosty

  • Flagrunner
  • ****
  • Posts: 601
  • Uber Desert Eagle ^^
Re: help with ReadFile
« Reply #5 on: February 18, 2012, 12:34:36 am »
thanks guys, now how would i read a full array to a writeconsole message or how to set the array length based on lines? the bug i have now is that it shows as it should but when the next server message pops up it comes up over the other lines of the writeconsole
check out my server! click here

If at first you don't succeed, Improvise! :D

Offline DorkeyDear

  • Veteran
  • *****
  • Posts: 1507
  • I also go by Curt or menturi
Re: help with ReadFile
« Reply #6 on: February 18, 2012, 07:49:20 am »
The problem you encountered is the behavior of WriteConsole with newlines. It expects only one line of text; otherwise the other lines will overlap any other text put into the player chat console. You would need to use WriteConsole multiple times, for each line, to avoid this.

To go from the content you read in a file to an array, the Explode function is what you want. File_array := Explode(File_data, CRLF); To get the number of lines in the file, you can use GetArrayLength(File_array). Some example code to spew out everything the server reads from a file (untested):
Code: [Select]
const
  CRLF = #13#10;

procedure SpewOut();
var
  filearraylength, i: integer;
  filename, filedata: string;
  filearray: array of string;
begin
  filename := 'scripts/' + ScriptName + '/Note.txt';
  filedata := ReadFile(filename);
  filedata := Copy(filedata, 0, Length(filedata) - 2);
  filearray := Explode(filedata, CRLF);
  filearraylength := GetArrayLength(filearray);
  WriteConsole(0, 'Length of array: ' + InttoStr(filearraylength), $FFFFFFFF);
  if (filearraylength > 0) then
    for i := 0 to filearraylength - 1 do
      WriteConsole(0, '[' + InttoStr(i) + '] ' + filearray[i], $EE81FAA1);
end;

This is only sample code. As recommended previously, it is a good idea to read the file only once before hand, and have the 'spew out' code not reload the data.
« Last Edit: February 18, 2012, 07:52:35 am by DorkeyDear »