Pages

Wednesday, November 11, 2015

Pascal Program to Create and Save Records in a Textfile

program Create_Records;
Type
Gra = String [30];
File_Content = Record
Name : Gra;
Matric_No : Gra;
Dept : Gra;
Sex : Gra;
Date_Of_Birth : Gra;
(*enter as many data as you like and intialise either as string, integer or char*);
End;

Var
 
       FileInfo : File_Content;
       Newfile : text;
        I : Integer;

Begin
Assign (newfile, 'Grace_Joseph.txt');
rewrite (Newfile);
      for i := 1 to 5 do
       begin
      writeln ('Enter name:');
      readln ( fileinfo.name);
       writeln ('matric number');
       readln (fileinfo.matric_No);
        writeln ('department');
       readln (fileinfo.dept);
        writeln ('sex');
        readln (fileinfo.sex);
        writeln ('date of  birth');
        readln (fileinfo.date_of_birth);
(*follow the above procedure to write and read the data you initialised*    )

writeln (newfile,' Name:', fileinfo.name)
writeln (newfile, 'matric number:', fileinfo.matric_no);
writeln (newfile,' Department: ', fileinfo.dept);
writeln (newfile,' Sex:', fileinfo.sex);
writeln (newfile,' Date Of Birth:', fileinfo.date_of_birth);
readln;
end;
close (newfile);
end.

In the program above, students records are saved in a textfile. You can rename the name of the textfile from Grace_Joseph.txt to yourDesired Name.txt. Goto the documents in your computer and search for the file using. the filename.txt.

The program creates a record for 5 students using the For Do loop.