Originally Posted by
Andrmolja
In sript
Simba Code:
program TryExceptFinallyExample;
(**
* Will write the string 'Text' to the file 'FileName'
* There are several procedures/functions I use in this example that you've
* never seen before. Don't panic! Click on the triangle beside 'Files' in the
* functions list and you'll see them all
*)
function WriteToFile(Text, FileName: string): Boolean;
var
thePath: string;
theFile: Integer; // When working with files, they are always of the Integer type
begin
// When setting the path, be sure you always end with a '\', otherwise you will get errors
thePath := AppPath + 'Scripts\'; // This will save the file in Simba/Scripts (AppPath = Application Path)
try
theFile := RewriteFile(thePath + FileName, False); // Saves the file to thePath and names it 'FileName'
except // An exception would be thrown most likely if there was an invalid file name
WriteLn('Error opening file!');
finally
WriteLn('Done trying to open file');
end;
// Notice how I assigned the opened or created file to 'theFile', this is so we can use it later in the procedure
if (WriteFileString(theFile, Text)) then
Result := True;
CloseFile(theFile); // Remember to ALWAYS close the file when you're finished! If you don't it can cause memory leaks and your script will run very slowly
end;
begin
ClearDebug;
if (WriteToFile('Test!', 'TestFile.txt')) then
WriteLn('Successfully wrote to file!')
else
WriteLn('Error writing to file');
end.
Open Simba/Scripts/TestFile.txt and a notepad file should open that looks something like this: Test!
I need looks something like this: Test! Test! Test! Test! Test! Test! Test! Test!...
For example: X := 123456...n, I need Write to file X-time string Test!
Here's some code that might help you. I'm not used to using for loops in Simba because I prefer Java, but I think that repeat loops are good enough. If you have trouble following the code, just let me know.
Simba Code:
program TryExceptFinallyExample;
var
input : string;
x, i : integer;
(**
* Will write the string 'Text' to the file 'FileName'
* There are several procedures/functions I use in this example that you've
* never seen before. Don't panic! Click on the triangle beside 'Files' in the
* functions list and you'll see them all
*)
function WriteToFile(Text, FileName: string): Boolean;
var
thePath: string;
theFile: Integer; // When working with files, they are always of the Integer type
begin
// When setting the path, be sure you always end with a '\', otherwise you will get errors
thePath := AppPath + 'Scripts\'; // This will save the file in Simba/Scripts (AppPath = Application Path)
try
theFile := RewriteFile(thePath + FileName, False); // Saves the file to thePath and names it 'FileName'
except // An exception would be thrown most likely if there was an invalid file name
WriteLn('Error opening file!');
finally
WriteLn('Done trying to open file');
end;
// Notice how I assigned the opened or created file to 'theFile', this is so we can use it later in the procedure
if (WriteFileString(theFile, Text)) then
Result := True;
CloseFile(theFile); // Remember to ALWAYS close the file when you're finished! If you don't it can cause memory leaks and your script will run very slowly
end;
begin
ClearDebug;
//EDIT THIS
x := 5;
//END EDIT
i := 0;
input := '';
repeat
input := input + 'Test!';
inc(i);
until i >= x;
if (WriteToFile(input, 'TestFile.txt')) then
WriteLn('Successfully wrote to file!')
else
WriteLn('Error writing to file');
end.