gpt4 book ai didi

linux - 适用于 Linux 的 Delphi Tokyo : stream file contents to stdout (console output)

转载 作者:塔克拉玛干 更新时间:2023-11-03 01:19:05 24 4
gpt4 key购买 nike

我已经研究了一段时间如何将文件内容(在本例中为 excel 文件)流式传输到控制台输出。为 Windows 编译时,使用 THandleStream 结合 STDOUT(控制台输出)句柄就像在公园里散步一样。它使用 Win32 API,因此在为 Linux(顺便说一下 Debian)编译时显然不会工作。

我正在寻找与此等效的内容:

...
aFileStream := TFileStream.Create(FullFileName,fmOpenRead); // This creates the input stream
aOutputStream := THandleStream.Create(GetStdHandle(STD_OUTPUT_HANDLE)); // Here goes the output stream
aOutputStream.CopyFrom(aFileStream, aFileStream.Size); // And the copy operation
...

更新:以下似乎适用于 Windows,但是一旦我切换到 Linux,它就无法编译,因为 PAnsiChar 未知。

...
f : File;
Buff : PAnsiChar;
BytesRead : integer;
ByteSize : integer;
iBuffRunner : integer;
...
AssignFile(F, 'details.xlsx');
Reset(F,1);
ByteSize := (fileSize(F));
GetMem(Buff,ByteSize);
BlockRead(F,Buff[0],ByteSize,BytesRead);
CloseFile(F);
for iBuffRunner := 0 to (Bytesize-1) do
System.Write(Buff[iBuffRunner]);
FreeMem(Buff);

你能想出一些可能有用的东西吗?

更新:

嗨,雷米,

再次感谢您的帮助!我快到了,还在为最后一点挣扎。您提到我应该能够对 system.output 执行 BlockWrite。但是,BlockWrite 期望 var F: File 作为第一个参数,而 System.Output 是 TEXT 类型?

此外,对于我正在阅读的文件,我正在使用“字节文件”而不是"file",我不确定如何正确转换以输出到控制台?

这是这个 linux POC 的当前状态:

这部分工作正常:阅读 details.xlsx 并将内容写入 test.xlsx(基本上是一个副本)。目标文件与源文件相同。

这部分还没有工作,但最终是我需要的:将 details.xlsx 的内容写入标准输出:

const
MaxBufSize = 4096;
var
f : File of Byte;
tf : File of Byte;
Buff : array of Byte;
BytesRead : integer;
ByteSize : integer;
WillRead : integer;
begin
AssignFile(F, 'details.xlsx');
Reset(F);
ByteSize := (fileSize(F));

if ByteSize > MaxBufSize then
BytesRead := MaxBufSize
else
BytesRead := ByteSize;
SetLength(Buff, BytesRead);

AssignFile(tf, 'test.xlsx');
Rewrite(tf);

try
while ByteSize <> 0 do
begin
if ByteSize > BytesRead then
WillRead := BytesRead
else
WillRead := ByteSize;
BlockRead(F,Buff[0], WillRead);
BlockWrite(tf,Buff[0], WillRead);
//BlockWrite(System.Output, buff[0], WillRead);
Dec(ByteSize, WillRead);
end;
finally
SetLength(Buff,0);
CloseFile(f);
CloseFile(tf);
end;
System.Readln;
end;

最终更新:

const
MaxBufSize = 4096;
var
f : File of Byte;
tf : File of Byte;
Buff : array of Byte;
BytesRead : integer;
ByteSize : integer;
WillRead : integer;
iBufRunner : integer;
begin
AssignFile(F, 'details.xlsx');
Reset(F);
ByteSize := (fileSize(F));

if ByteSize > MaxBufSize then
BytesRead := MaxBufSize
else
BytesRead := ByteSize;
SetLength(Buff, BytesRead);

AssignFile(tf, 'test.xlsx');
Rewrite(tf);

try
while ByteSize <> 0 do
begin
if ByteSize > BytesRead then
WillRead := BytesRead
else
WillRead := ByteSize;
BlockRead(F,Buff[0], WillRead);
BlockWrite(tf,Buff[0], WillRead);
for iBufRunner := 0 to (WillRead - 1) do
System.Write(System.Output, UTF8Char(Buff[iBufRunner]));
Dec(ByteSize, WillRead);
end;
finally
SetLength(Buff,0);
CloseFile(f);
CloseFile(tf);
end;
System.Readln;
end;

最佳答案

我们找到了解决方案:使用 UTF8Char 而不是 AnsiChar 达到了目的。 (参见最终更新)

关于linux - 适用于 Linux 的 Delphi Tokyo : stream file contents to stdout (console output),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46293975/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com