Can scar read or save txt files ?
Can scar read or save txt files ?
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.
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.
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 ;
[Runtime Error] : List index out of bounds (229) in line 14 in scriptCode: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.
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
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!.
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.
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.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.
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.
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!!!
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.
There are currently 1 users browsing this thread. (0 members and 1 guests)