dopeness Posted July 13, 2012 Share Posted July 13, 2012 Hello again, I am trying to figure out how to parse through data. I wanted to just store it into a string, but do to the lack of basic string functions i couldn't. So now I tried just saving it to a file and then reading the file line by line, but scar doesn't even have a function to read line by line, instead it just stores everything into a string which brings me back to square one. So, how would i go about sifting through a bunch of data in a string and pull out of it what i need. Thanks a lot guys. Quote Link to comment Share on other sites More sharing options...
Janilabo Posted July 13, 2012 Share Posted July 13, 2012 (edited) Hello, just made couple functions that you might find useful.. CountFileLines() and GetFileLine(). I included an example for you. Hopefully they help you out. -Jani [scar]function CountFileLines(path: string): Integer; var f: Integer; s: string; begin if (path <> '') then if FileExists(path) then begin f := OpenFile(path, False); ReadFileString(f, s, FileSize(f)); CloseFile(f); Result := (CountStr(#13#10, s) + 1); end; end; function GetFileLine(path: string; line: Integer): string; var f: Integer; s: string; TSA: TStringArray; begin if ((path <> '') and (line > 0)) then if FileExists(path) then begin f := OpenFile(path, False); ReadFileString(f, s, FileSize(f)); CloseFile(f); TSA := Explode(#13#10, s); if (Length(TSA) >= line) then Result := TSA[(line - 1)]; SetLength(TSA, 0); end; end; var clPath: string; clc, i: Integer; begin clPath := (Replace(AppPath, 'bin\', '') + 'changelog.txt'); clc := CountFileLines(clPath); WriteLn('Changelog contains ' + IntToStr(CountFileLines(clPath)) + ' lines!'); WriteLn('Printing changelog LINE-BY-LINE in 3 seconds!'); Wait(3000); for i := 1 to clc do WriteLn(GetFileLine(clPath, i)); end.[/scar] Edit: Here is another style to get data from line (GetStrLine() + ReadFileStr()), check below.. [scar]function GetStrLine(str: string; line: Integer): string; var TSA: TStringArray; begin if ((str <> '') and (line > 0)) then begin TSA := Explode(#13#10, str); if (Length(TSA) >= line) then Result := TSA[(line - 1)]; SetLength(TSA, 0); end; end; function ReadFileStr(Path: string): string; var f: Integer; begin if not FileExists(Path) then Exit; f := OpenFile(Path, False); ReadFileString(f, Result, FileSize(f)); CloseFile(f); end; var str, clPath: string; c, i: Integer; begin ClearDebug; clPath := (Replace(AppPath, 'bin\', '') + 'changelog.txt'); str := ReadFileStr(clPath); c := (CountStr(#13#10, str) + 1); WriteLn('Changelog contains ' + IntToStr© + ' lines!'); WriteLn('Printing changelog LINE-BY-LINE in 3 seconds!'); Wait(3000); for i := 1 to c do WriteLn(GetStrLine(str, i)); str := ''; end.[/scar] Of course, the best way is just to store string lines as TStringArray (or TStringList). Then it is wayyy quicker to get the lines, because there wont be need for loading the data again every single time (like with these functions). Edited July 13, 2012 by Janilabo Improved GetFileLine() & GetStrLine() a bit. Quote Link to comment Share on other sites More sharing options...