Results 1 to 2 of 2

Thread: Reading Strings from Files on SCAR

  1. #1
    Join Date
    Sep 2006
    Posts
    2
    Mentioned
    0 Post(s)
    Quoted
    0 Post(s)

    Default Reading Strings from Files on SCAR

    Closed

  2. #2
    Join Date
    Oct 2006
    Location
    I'm a figment of your imagination
    Posts
    422
    Mentioned
    0 Post(s)
    Quoted
    0 Post(s)

    Default

    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!!!

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Replies: 15
    Last Post: 09-22-2008, 12:32 PM
  2. C++: Writing and reading files
    By Repentinus in forum C/C++ Help and Tutorials
    Replies: 3
    Last Post: 11-11-2007, 04:55 PM
  3. [Scar IDE] How to set jEDIT to edit scar files corectly!
    By LordGregGreg in forum Outdated Tutorials
    Replies: 16
    Last Post: 06-23-2007, 01:19 AM
  4. SCAR Divi 3.01 DONT associate .scar files!!!
    By chimpy in forum News and General
    Replies: 1
    Last Post: 04-21-2007, 08:49 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •