0

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?

Arnthor
  • 2,563
  • 6
  • 34
  • 54

2 Answers2

2

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;
ninjalj
  • 42,493
  • 9
  • 106
  • 148
  • 2
    You can't append() to file of custom records. You can reset() file and change pointer to the end of it, though. Anyway, problem's solved, thanks for help. – Arnthor May 11 '11 at 10:12
  • @Nordvind since your problem is solved, it would be polite to select some of the answers as the correct one by clicking on the big "V" at left :) – brandizzi Jul 25 '11 at 13:05
1

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!

No'am Newman
  • 6,395
  • 5
  • 38
  • 50