Pascal - 不写入文件

发布于 2024-11-06 15:57:03 字数 705 浏览 0 评论 0原文

帕斯卡大师们好! 我有一个自定义记录的文件类型:

DBCell = record
    Name: string[10];
    Surname: string[15];
    Balance:integer;
    OpenDate: record
        year: integer;
        month: 1..12;
        day:1..31
    end;
    AccountN: string[10];
end;
DBFile = file of DBCell;

以及打开并向文件添加新元素的函数:

procedure Fopenf(var F:DBFile; var FName:string; var FOpened:boolean);
begin
    Assign(F,FName);

    rewrite(F);

    FOpened:=true;
end;

procedure InsN(var F:DBFile;var cell:DBCell;var FOpened:boolean);
begin
        Write(F,cell);
        Close(F);
        Rewrite(F);
        Writeln('Added');
        FOpened:=false;
end;

问题是,实际上没有任何内容写入文件。我做错了什么?

Howdy, Pascal masters!
I've got a file type of custom records:

DBCell = record
    Name: string[10];
    Surname: string[15];
    Balance:integer;
    OpenDate: record
        year: integer;
        month: 1..12;
        day:1..31
    end;
    AccountN: string[10];
end;
DBFile = file of DBCell;

And functions, that open and add new element to file:

procedure Fopenf(var F:DBFile; var FName:string; var FOpened:boolean);
begin
    Assign(F,FName);

    rewrite(F);

    FOpened:=true;
end;

procedure InsN(var F:DBFile;var cell:DBCell;var FOpened:boolean);
begin
        Write(F,cell);
        Close(F);
        Rewrite(F);
        Writeln('Added');
        FOpened:=false;
end;

Problem is, nothing is actually written to file. What am I doing wrong?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

囚我心虐我身 2024-11-13 15:57:03

我已经很久没有使用 Pascal 了,但是 IIRC Rewrite 截断了该文件。您应该使用Append

在文件中插入记录后,您不需要 Rewrite()

procedure InsN(var F:DBFile;var cell:DBCell;var FOpened:boolean);
begin
    Write(F,cell);
    Close(F);
    Writeln('Added');
    FOpened:=false;
end;

如果您不想每次打开文件时都截断文件:

procedure Fopenf(var F:DBFile; var FName:string; var FOpened:boolean);
begin
    Assign(F,FName);

    append(F);

    FOpened:=true;
end;

It's been a long time since I've done any Pascal, but IIRC Rewrite truncates the file. You should use Append.

You don't need the Rewrite() after inserting a record in the file:

procedure InsN(var F:DBFile;var cell:DBCell;var FOpened:boolean);
begin
    Write(F,cell);
    Close(F);
    Writeln('Added');
    FOpened:=false;
end;

If you don't want to truncate the file every time you open it:

procedure Fopenf(var F:DBFile; var FName:string; var FOpened:boolean);
begin
    Assign(F,FName);

    append(F);

    FOpened:=true;
end;
黯然 2024-11-13 15:57:03

问题是 InsN 中的“重写”调用。 “重写”创建一个新文件,因此通过在程序末尾调用它,您将创建一个新的空文件!

The problem is the 'rewrite' call in InsN. 'Rewrite' creates a new file, so by calling it at the end of your program, you are creating a new, empty file!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文