Dear Sir,

Please help me in this part of the question.

I want to change the phone a friend details. Find a person and change their phone a friend number. To do this I need to find a record in memory and overwrite that record's phone field. Then save all the records back to the file. The next time I run the program I run the program I should be able to see the person's new phone number.

Cheers,

Dani AI

Generated

wanted to update a stored "phone-a-friend" record and provided a working typed-file example that reads records, lets the user change one in memory, and writes it back. That approach is fine for small utilities and learning Pascal file I/O. A few practical tips will make it safer and more future-proof.

Treat phone numbers as text, not integers. Numeric types drop leading zeros, lose formatting (dashes, plus signs, extensions) and can overflow for long numbers. For typed files, use fixed-size storage (ShortString / fixed char arrays) rather than dynamic strings, because dynamic string fields write pointers, not the text contents. Always validate the selected record index against the file size before Seek/Write to avoid corrupting the file. Create a backup (or write to a temporary file and rename) before overwriting the original. Use packed records if you need predictable on-disk layout, and be aware that record layout and pointer sizes can change between compiler versions or between 32/64-bit builds — typed files are not ideal for long-term, cross-version storage.

Add basic I/O error handling and input validation (numeric-only check if you accept digits, max length check, trim spaces). For anything beyond a tiny data set, consider a simple CSV/JSON file or an embedded database (SQLite) for better portability, searchability, and robustness. These steps preserve the convenience of 's method while avoiding common pitfalls and data loss.

Dear Sir,
Please help me in this part of the question.
I want to change the phone a friend details. Find a person and change their phone a friend number. To do this I need to find a record in memory and overwrite that record's phone field. Then save all the records back to the file. The next time I run the program I run the program I should be able to see the person's new phone number.
Cheers,

Sorry for wrong answer.I wrote to you that you have to use the blockread and blockwrite commands.When I'm learning more about the typed file I changed my opinion.The result now is a working program.You have to use typed file like your record.Cause only typed of record elements can be write to that file :)

{
Question: Typed Files;

[ 0.data  ]
[ 1.data  ]
[ 2.data  ]
[ ...     ]
[ n.data  ]

We can reach this file's elements row by row,or directly
}


Program Project2;

{$APPTYPE CONSOLE}

Uses
  SysUtils;

Type
    St15 = String[15];
    ConData = Record
                 F_Name:St15;
                 L_Name:St15;
                 P_Number:LongInt;
              End;
Var
    OneP:ConData;
    Person:Array[0..2]of ConData;
    Guy:Array[0..2]of ConData;
    OurFile:File Of ConData; {file type is important!}
    i:Byte; //0..255
    u:Byte;

Procedure Stars;
Var a:Byte;
Begin
   For a:=1 To 40 Do Write('*');
   WriteLn;
End;

Begin {main}
   //Fill 3 person's data
   For i:=0 To 2 Do Begin
      Stars;
      Write(i:2,'.First name: ');
      ReadLn(Person[i].F_Name);
      Write('   Last name: ');
      ReadLn(Person[i].L_Name);
      Write('   Her/His phone number: ');
      ReadLn(Person[i].P_Number);
      If (i=2)Then Begin
         Stars;
      End;
   End;
   //assigning
   Assign(OurFile,'C:\RecFile.Dat');
   //creating this file
   Rewrite(OurFile);
   //writing the records to file
   For i:=0 To 2 Do Begin
      Write(OurFile,Person[i]);
   End;
   WriteLn;
   WriteLn('Show the contents of the file:');
   WriteLn;
   Reset(OurFile);
   i:=0;
   //Fill the array with records,reading from file
   While (EoF(OurFile)=False)Do Begin
    Read(OurFile,Guy[i]);
    Inc(i);
   End;
   //write the elements of the array
   For i:=0 to 2 do begin
      Write(i,'. Name: ',Guy[i].F_name,' ');
      Write(Guy[i].L_name,'   Phone: ');
      WriteLn(Guy[i].P_number);
      WriteLn;
   End;
   WriteLn;
   Write('Please give me the member''s number.0 or 1 or 2 : ');
   ReadLn(u);
   WriteLn;
   Seek(OurFile,u);//positioning
   Read(OurFile,OneP); //read one record from file
   Write(OneP.F_name,' ');
   Write(OneP.L_name,' ');
   WriteLn('last created phone number: ',OneP.P_number);
   WriteLn;
   Write('Now,give me her/his new phone number: ');
   ReadLn(OneP.P_number);//and here changes the number by the user
   WriteLn;
   Seek(OurFile,u);//positioning
   Write(OurFile,OneP);//write the new data
   WriteLn;
   WriteLn('Read again that file: ');
   WriteLn;
   Reset(OurFile); //open for reading
   i:=0;
   //Fill the array with records,reading from file
   While (EoF(OurFile)=False)Do Begin
    Read(OurFile,Guy[i]);
    Inc(i);
   End;
   //write the elements of the array
   For i:=0 to 2 do begin
      Write(i,'. Name: ',Guy[i].F_name,' ');
      Write(Guy[i].L_name,'   Phone: ');
      WriteLn(Guy[i].P_number);
      WriteLn;
   End;
   //closing the file
   Close(OurFile);
   ReadLn;
End.{end of main}

//Tested in Delphi 7,working!

{
-=Created by FlamingClaw 2009.04.20=-
}

:D

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.