Closed
Closed
What you need to do is, you need to make the script find the linebreak. In order to do that, you simply dump the contents of the file into a variable.
The linebreak character is invisible, but its ASCII-representation is 13 and another character I haven't figured out yet. Anyways, make the script search for that #13 and then make it delete everything before this. Put this in a loop and loop it as many times as the line you want to extract. Once you have the line, make the script delete everything after the next #13. Then you've isolated the string.
SCAR Code:program ReadLines;
//Reads a file
function ReadFile(RelativePath : string) : string;
begin
FileNo := OpenFile(ScriptPath + RelativePath, true);
If FileNo > -1 then
begin
If not(ReadFileString(FileNo, Result, FileSize(FileNo))) then
Result := 'error';
CloseFile(FileNo);
end else Result := 'error'
end;
//Writes a new file or overwrites existing one
function WriteFile(RelativePath, Content : string) : boolean;
begin
Result := true;
FileNo := RewriteFile(ScriptPath + RelativePath, true);
If FileNo > -1 then
begin
If not(WriteFileString(FileNo, Content)) then
Result := false;
CloseFile(FileNo);
end else Result := false;
end;
//Adds to an existing file
function AppendFile(RelativePath, Content : string) : boolean;
begin
Result := true;
If not(ReadFile(RelativePath) = 'error') then
WriteFile(RelativePath, (ReadFile(RelativePath) + Content))
else Result := false;
end;
//Reads a line out of a file specified by line
function ReadFileLine(RelativePath : string; line : integer) : string;
var
currentline : integer;
filebuffer : string;
begin
currentline := line;
filebuffer := ReadFile(RelativePath);
If line = 0 then
begin
delete(filebuffer, pos(#13, filebuffer), length(filebuffer))
Result := filebuffer
end else
begin
repeat
delete(filebuffer, 1, pos(#13, filebuffer));
currentline := currentline - 1;
until(currentline = 0);
delete(filebuffer, pos(#13, filebuffer), length(filebuffer));
Result := filebuffer;
end;
end;
//Gets number of lines in a file. Always returns 1 if nothing in file
function GetFileLines(RelativePath : string) : integer;
var
filebuffer : string;
begin
filebuffer := ReadFile(RelativePath);
repeat
delete(filebuffer, 1, pos(#13, filebuffer));
Result := Result + 1;
until(pos(#13, filebuffer) = 0);
Result := Result + 1;
end;
begin
ReadFileLines('yourfilehere.dat', 2); //Reads line #2 out of yourfilehere.dat
end.
It's been a while... but I'm BACK!!!
There are currently 1 users browsing this thread. (0 members and 1 guests)