Pascal - 不写入文件
帕斯卡大师们好! 我有一个自定义记录的文件类型:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我已经很久没有使用 Pascal 了,但是 IIRC
Rewrite
截断了该文件。您应该使用Append
。在文件中插入记录后,您不需要
Rewrite()
:如果您不想每次打开文件时都截断文件:
It's been a long time since I've done any Pascal, but IIRC
Rewrite
truncates the file. You should useAppend
.You don't need the
Rewrite()
after inserting a record in the file:If you don't want to truncate the file every time you open it:
问题是 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!