Pascal Program to Create and Save Records in a Textfile

The program below is a Pascal Program code example to create and save records in a Textfile.

Here, we'll be using the FOR DO looping statement in Pascal to create a record for 5 students.

Each record is to contain fields such as Name, Matric Number, Department, Sex, and Date of Birth; you can as well increase the number of fields by editing the global declaration section of this code.

The syntax of the FOR DO loop in Pascal is given by:

FOR (I=Start TO End) DO

This sets the minimum and the maximum number of students we require in this record, where it can't be less or more because of this repetition statement.

After the record has been created and the user data has been entered, the text file is stored directly in your Hardrive, hence you'll have to navigate to the documents folder in your computer's C or D drive.


Program Code


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.


Logic Of the Program


In the program above, students records are created and saved in a text file, and here's the logic behind it.

  • First, we follow the same syntax for creating a record in Pascal Programming Language by declaring it.
  •  Next, we create variables like Name, Matric Number, Sex, Date of Birth and set to a string (GRA) data type because we'll be dealing with alphabets, characters, and numbers here.
  • The next section of the program defines the variable 'New File' as a text file that will be called Grace_Joseph.txt in the next section.
  • You can rename the name of the textfile from Grace_Joseph.txt to yourDesired Name.txt.

Now,

  • The program creates a record for 5 students using the For Do loop variable 'i'. 
  • Once a record has been entered completely for the first student, 'i' is incremented until we have records for all 5 students.


Conclusion:


To check if the file has been created, go to the documents in your computer and search for the file using. the filename.txt.

If you have questions or encounter problems while creating a student's record in Pascal, you can leave a comment below and work with you to resolve it.


Also Read:

BASIC Program For Sum of Numbers Using IF Then Statement
Pascal Arithmetic Program Using IF THEN statement
Pascal Program to Create and Save Records in a Textfile
Pascal program To Calculate Area of a Triangle
 Pascal Program of Matrix Multiplication
Pascal Program to Find The Average Of Only Positive Numbers Using While Do loop

Do you like this post? Please link back to this article by copying one of the codes below.

URL Of Post:


HTML Link Code:

BB (forum) link code:

Comments