windows - 重置非空文件后,Freepascal 发现 eof

标签 windows reset eof freepascal

让我难住了:在 Windows 上,Free Pascal

    {... writes text lines to PdfTmp, then ...}
    close(PdfTmp);
    reset(PdfTmp);
    while not eof(PdfTmp) do begin
        readln(PdfTmp,InpLine);
        writeln(ProdFile,InpLine);
    end;
    close(PdfTmp);

我已经验证 PdfTmp 文件是用文本写入的,但是 eof() 函数在第一次调用时返回 true,因此永远不会执行 while block 。

我对周围的代码尝试了各种技巧来确定可能导致失败的其他原因,包括更新我的 FPC 编译器,但都无济于事。测试确认这是不正确的 eof() 函数结果。

相同的代码在 Mac 上可以正常工作。 (FreePascal 支持各种平台。)

有没有其他可怜的人遇到过这种不幸并拖延了一个不错的项目?如果是,它是如何修复的?

最佳答案

这是我解决问题的方法:

{}
{$ifdef WIN}

procedure starttext (var F :file; var Feof :boolean);
begin
    reset(F,1); Feof := filesize(F) = 0;
end;

procedure gettext (var F :file; Feoln :array of byte; var Feof :boolean; var S :string);
{ gets the next text line from F into S. Returns false at end of file.
It returns the last string and sets eof if there are no additional strings.}
var  endline, endfile :boolean;  P, R :longint; C, D :byte;
begin
    S := '';
    endline := false;
    endfile := false;
    repeat
        P := filepos(F);
        blockread(F,C,1,R);
        if R = 0 then begin
            endline := true;
            endfile := true; end
        else begin
            P := P + 1;
            if C = Feoln[0] then begin
                if high(Feoln) > 0 then begin
                    blockread(F,D,1,R);
                    if (R <> 0) then begin
                        if D = Feoln[1] then begin
                            endline := true;
                            P := P + 1; end
                        else
                            seek(F, P);
                    end else
                        P := P + 1;
                end else
                    endline := true;
            end else
                 S := S + chr(C);
            endfile := P = (filesize(F));
        end;
    until
        (endline = true) or (endfile = true);
    Feof := endfile;
end;

{$endif}
{}
...
{}
        close(PdfTmp);
{$ifdef OSX}
        reset(PdfTmp);
        while not eof(PdfTmp) do begin
            readln(PdfTmp,InpLine);
            putpdfln(InpLine);
        end;
        close(PdfTmp);
{$endif}
{$ifdef WIN}
        assign(PdfWrk,FileID+'.$$$'); {same file as PdfTmp}
        starttext(PdfWrk,eofPdfWrk);
        while not eofPdfWrk do begin
            gettext(PdfWrk, [13,10], eofPdfWrk, InpLine);
            putpdfln(InpLine);
        end;
        close(PdfWrk);
{$endif}
{}
...

当我意识到 Microsoft 未能遵循 IBM 在第一台 IBM-PC 上发布的关于如何处理中断的说明时,我放弃了 Windows 是一个可行产品的想法。这种失败仍然困扰着 Windows 的可怕的错误类结构。并导致我们在 Windows 应用程序中都遇到过的神秘偶发故障。

就我个人而言,我认为操作系统不应该使用类来构建,这会给系统中只需要一次的进程代码带来不必要的开销(如果处理得当,系统中的所有代码都将如此)操作系统)。一个好的 API 就足够了。

关于windows - 重置非空文件后,Freepascal 发现 eof,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35228009/

相关文章:

windows - 如何在 Windows 上编译 lex 文件?

windows - 用于在子文件夹中查找文件夹并获取路径的批处理脚本

c# - 如何使用 C# 应用程序重置 Arduino Mega2560?

javascript - 如何只重置文本框,而不是整个页面?

jquery - 重置jquery分页插件中的总页数

sql - 'select'缺少EOF错误

windows - Win32 : CreateDialog instead of multiple calls to CreateWindow - any downsides?

python - 如何使用通知上有按钮的python发送Windows 10通知

c++ - 通过 C++ 程序中 EOF (CTRL+D) 字符的错误代码提供用户反馈

c - 看来我必须在 getchar() 获取 EOF 之前先按回车键