简体   繁体   English

DWScript写入/读取简单的文本文件

[英]DWScript Write/Read a simple text file

I would like to write/read a simple text file using dwscript. 我想使用dwscript编写/读取一个简单的文本文件。 My code is here below... but I am non able to get it run, please someone might help...: (I am using the Simple.exe in the Demos folder of DWS installation) 我的代码在下面...但是我无法运行它,请有人帮忙... :(我正在DWS安装的Demos文件夹中使用Simple.exe)

// uses Classes;
{$INCLUDE_ONCE 'c:/.../System.Classes.pas'}

var
    s: TFileStream;
    o: string; // out
    i: integer;
    f: word; // flag

f := fmOpenReadWrite;
if not FileExists('C:\Temp\Junkfile.txt') then
    f := f or fmCreate;

s := TFileStream.Create('C:\Temp\Junkfile.txt', f);
try
    s.Position := s.Size;  // will be 0 if file created, end of text if not
    for i := 1 to 10 do begin
        o := Format('This is test line %d'#13#10, [i]);
        s.Write(o[1], Length(o) * SizeOf(Char));
    end;
finally
    s.Free;
end;

By default the script engine keeps everything sand-boxed and nothing that gives access outside the sandbox is exposed. 默认情况下,脚本引擎会将所有内容保留在沙盒中,并且不会暴露任何可在沙盒外部进行访问的内容。 So if you want to give access to arbitrary files to script you need to expose functions & classes to achieve it (through TdwsUnit fi). 因此,如果要授予对脚本的任意文件访问权限,则需要公开函数和类以实现此目的(通过TdwsUnit fi)。

Also it won't compile the Delphi classes unit, DWScript is not meant to be an alternative to the Delphi compiler, but to offer scripting support, ie. 同样,它不会编译Delphi类单元,DWScript并不是Delphi编译器的替代,而是提供脚本支持。 allow end users to run code in a way over which you have full control over what they can do, and that can't crash or corrupt the host application (that last point being the key differentiation with the other notable Pascal scripting engines). 允许最终用户以完全控制他们可以做什么的方式运行代码,并且不会崩溃或破坏主机应用程序(最后一点是与其他著名的Pascal脚本引擎的关键区别)。

You can use dwsFileFunctions to get basic file I/O support, in which case an equivalent to the file creation portion of your code would be something like 您可以使用dwsFileFunctions获得基本的文件I / O支持,在这种情况下,相当于代码中文件创建部分的内容类似于

var f := FileCreate('C:\Temp\Junkfile.txt');

for var i := 1 to 10 do
   FileWrite(f, Format('This is test line %d'#13#10, [i]));

FileClose(f);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM