开发者

Pascal - not writing to file

开发者 https://www.devze.com 2023-03-04 19:24 出处:网络
Howdy, Pascal masters! I\'ve got a file type of custom records: DBCell = record Name: string[10]; Surname: string[15];

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
  开发者_JAVA百科  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?


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;


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!

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号