Results 1 to 9 of 9

Thread: Read and save txt files

  1. #1
    Join Date
    Feb 2007
    Posts
    25
    Mentioned
    0 Post(s)
    Quoted
    0 Post(s)

    Default Read and save txt files

    Can scar read or save txt files ?

  2. #2
    Join Date
    Dec 2006
    Location
    Copy pastin to my C#
    Posts
    3,788
    Mentioned
    8 Post(s)
    Quoted
    29 Post(s)

    Default

    SCAR can read and save any files (exes?), because they all have source code.

    My script that downloads SRL from www.srl-forums.com/srl-repos/ can download the plugins too because if you do GetPage to like http://www.srl-forums.com/srl-repos/...izzyPlugin.dll, it get's the source code.

    So, to the business.

    OpenFile gives you the file number, ReWriteFile gives you the file number and empties the file, - rewrites.

    So, after you have the number, use WriteFileString or what you want to do...

    Though you should've had a look at SCAR manual before asking...

    File functions

    function OpenFile(Path: string; Shared: Boolean): Integer;
    Opens file for reading and returns file number if successful or negative number if failed. If shared = true then attempts to open files used by other applications as read-only.

    function RewriteFile(Path: string; Shared: Boolean): Integer;
    Opens file for writing and truncates it. Returns file number if successful or negative number if error.

    procedure CloseFile(FileNum: Integer);
    Closes file specified by FileNum.

    function EndOfFile(FileNum: Integer): Boolean;
    Returns true if end of file specified by FileNum reached.

    function FileSize(FileNum: Integer): LongInt;
    Returns file size in bytes.

    function WriteFileByte(FileNum: Integer; b: Byte): Boolean;
    Writes a single byte to file.

    function WriteFileInt(FileNum: Integer; i: Integer): Boolean;
    Writes a single integer to file.

    function WriteFileString(FileNum: Integer; s: string): Boolean;
    Writes a string to file.

    function ReadFileByte(FileNum: Integer; var b: Byte): Boolean;
    Reads a single byte from file.

    function ReadFileInt(FileNum: Integer; var i: Integer): Boolean;
    Reads a single integer from file.

    function ReadFileString(FileNum: Integer; var s: string; Length: Integer): Boolean;
    Reads Length number of bytes from file into string s.

    procedure WriteINI(Section, KeyName, NewString, FileName: string);
    Writes data to an ini-structured file.
    Structure:
    [Section]
    KeyName=NewString

    function ReadINI(Section, KeyName, FileName: string): string;
    Reads data from an ini-structured file.
    Structure:
    [Section]
    KeyName=SomeString

    procedure DeleteINI(Section, KeyName, FileName: string);
    Deletes a KeyName from an INI file and if you pass an empty string as KeyName it will delete then entire Section.

    function DirectoryExists(Directory: string): Boolean;
    Returns true if the directory exists.

    function FileExists(FileName: string): Boolean;
    Returns true if the file exists.

    function ExtractFileName(FileName: string): string;
    Returns the name of a file from a string.
    Example:
    C:\somefolder\somefile.ext => somefile.ext

    function ExtractFilePath(FileName: string): string;
    Returns the filepath from a string.
    Example:
    C:\somefolder\somefile.ext => C:\somefolder\

    function ExtractFileExt(FileName: string): string;
    Returns the file extension from a string.
    Example:
    C:\somefolder\somefile.ext => .ext

    function ExtractFileDrive(FileName: string): string;
    Returns the file drive from a string.
    Example:
    C:\somefolder\somefile.ext => C:

    function GetFiles(Path, Ext: string): TStringArray;
    Returns the files in a directory.

    function SaveToFile(Sender: TObject; FileName: string): Boolean;
    Saves the content of certain objects:
    TStrings, TStringList, TBitmap, TImage, TPicture, TMemo, TListBox, TComboBox, TRichEdit, TCheckListBox, TGraphic
    Returns True if the saving was succesful.

    function LoadFromFile(Sender: TObject; FileName: string): Boolean;
    Loads the content of certain objects:
    TStrings, TStringList, TBitmap, TImage, TPicture, TMemo, TListBox, TComboBox, TRichEdit, TCheckListBox, TGraphic
    Returns True if the loading was succesful.

    function MD5FromFile(Filepath: string): string;
    Returns the MD5 hash value of a file.

  3. #3
    Join Date
    Sep 2006
    Posts
    6,089
    Mentioned
    77 Post(s)
    Quoted
    43 Post(s)

    Default

    Easiest way to read/write textfiles is TStringList I think.

    SCAR Code:
    program New;
    var
      i: Integer;
      t: TStringList;
    begin
      t := TStringList.Create;
      LoadFromFile(t, 'SOMETEXTFILE');
      for i := 0 to t.Count -1 do
        WriteLn(t.Strings[i]);
    end.
    Hup Holland Hup!

  4. #4
    Join Date
    Feb 2007
    Posts
    25
    Mentioned
    0 Post(s)
    Quoted
    0 Post(s)

    Default

    SCAR Code:
    program New;
    begin
      Writeln (inttostr (OpenFile('C:\exemple.txt',  True)));
      Writeln (inttostr (RewriteFile('C:\exemple.txt',  False)));
    end.

    So shoudl I pot the file in the sacr directory becouse this always shows me 0 and -1 so something is not correct;

    And it shows some kind of file access error ; any ideas ?

    SCAR Code:
    program New;
    var
      i: Integer;
      t: TStringList;
    begin
      t := TStringList.Create;
      LoadFromFile(t, 'C:\exemple.txt');
      for i := 0 to t.Count -1 do
        WriteLn(t.Strings[i]);
    end.

    Your script nielsie95 works but thre is still a problem - how to save the string ?

    And another problem is that I cant work with TStringLists ;

    Code:
    program New;
    var
      i: Integer;
      t: TStringList;
      X: integer;
    begin
      t := TStringList.Create;
      LoadFromFile(t, 'C:\draugiem.htm');
      for i := 0 to t.Count -1 do
        WriteLn(t.Strings[i]);
      x := POS ('exemple', t.Strings[i]);
    end.
    [Runtime Error] : List index out of bounds (229) in line 14 in script

    Is there a way to convert that TStringList into string so I can work with it ?


    And nevermind ; Realised that its and array :P; got it working

  5. #5
    Join Date
    Feb 2007
    Location
    Het ademt zwaar en moedeloos vannacht.
    Posts
    7,211
    Mentioned
    26 Post(s)
    Quoted
    72 Post(s)

    Default

    The runtime error is because you don't have begin/end around the for-loop, and because that Scar will still increment the I even when not entering the loop.
    I made a new script, check it out!.

  6. #6
    Join Date
    Jan 2007
    Location
    Tennessee
    Posts
    642
    Mentioned
    0 Post(s)
    Quoted
    1 Post(s)

    Default

    Pressing f1 in SCAR or going to the help tab and clicking help can solve a lot of problems.
    It has a list of all the file functions in there too.

  7. #7
    Join Date
    Jun 2007
    Location
    Wednesday
    Posts
    2,446
    Mentioned
    3 Post(s)
    Quoted
    1 Post(s)

    Default

    SCAR Code:
    program fileTest;
    var
    myFile:integer;
    text:string;

    begin
      myFile := RewriteFile(ScriptPath+'testFile.txt',false);
      WriteFileString(myFile, 'Hello World! ');
      WriteFileString(myFile, 'I am writing '+chr(13));
      WriteFileString(myFile, 'inside a file ');
      CloseFile(myFile);

      myFile:=OpenFile(ScriptPath+'testFile.txt',false);
      ReadFileString(myFile,text,fileSize(myFile));
      writeln(text);
      CloseFile(myFile);
    end.
    I always use that as an outline when using file edits. You can use TStringList, but I find it best sticking to normal code rather than object based coding as it can be annoying when you hit an error. Also, this submits it into a string.
    If you still want to use TStringList, you could always replace the WriteLn with an assignment to the string var (so it's RandomStringName := RandomStringName + t.Strings[i] in the for loop.
    By reading this signature you agree that mixster is superior to you in each and every way except the bad ways but including the really bad ways.

  8. #8
    Join Date
    Nov 2006
    Location
    in a house on lakemichigan
    Posts
    21
    Mentioned
    0 Post(s)
    Quoted
    0 Post(s)

    Default I love you guys

    No Matter what I need for scar file I/O , finding buttons, or ideas on parsing data I can come here or freddies place and look for an instance of most any function and find a discussion that will help me find what I need. this topic here was VARY helpful didn't answer all my questions but gave me lots of food for thought. Thanks again!!!

  9. #9
    Join Date
    Jan 2008
    Location
    California, US
    Posts
    2,765
    Mentioned
    0 Post(s)
    Quoted
    0 Post(s)

    Default

    This puts the tstringlist into a regular string. But it dosen't add the returns so the text is all clumped together. I'm sure you will find a way to do that .

    SCAR Code:
    program New;
    var
      i: Integer;
      t: TStringList;
      s : string;
      ii : integer;
    begin
      t := TStringList.Create;
      LoadFromFile(t, '');
      for i := 0 to t.Count -1 do
      begin
        for ii := t.count - 1 downto 0 do
        begin
          s := s + t[i];
        end;
      end;
    end.

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. Replies: 0
    Last Post: 02-16-2009, 10:12 AM
  2. Save and Open commands.
    By jhildy in forum OSR Advanced Scripting Tutorials
    Replies: 10
    Last Post: 12-29-2007, 08:08 PM
  3. Save Debug to file?
    By sucidal_boredaholic in forum OSR Help
    Replies: 2
    Last Post: 12-11-2007, 05:03 AM
  4. How to Use SRL succesfully and where to save it
    By Sdcit in forum Outdated Tutorials
    Replies: 30
    Last Post: 09-06-2007, 12:54 AM

Posting Permissions

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