Sunday, June 27, 2021

Programming – Pascal: Essential skills 5

 Programming – Pascal: Essential skills 5

 

Learning Outcomes:

  • Manipulate text files through filehandling statements.
    • The manipulation involves file updating statements to delete, insert, append and amend records.
       

Text file handling


Read from file

var

                infile : text;         (* text file variable *)

                a : integer;

begin

                assign (infile, ‘yourfile.txt’);         (* step 1: associate the file with the variable *)

                reset (infile);                                      (* step 2: read from beginning *)

                while not eof (infile) do

                begin

                                readln (infile, a);                (* step 3: read the integer and put into a *)

                                writeln (a)                           (* write the content on the screen *)

                end;

                close (infile)                                         (* step 4: close the file properly *)

end;

 

 

Write into file

var

                infile : text;         (* text file variable *)

                a : integer;

begin

                assign (infile, ‘yourfile.txt’);         (* step 1: associate the file with the variable *)

                rewrite (infile);                                 (* step 2: create or erase the file for writing*)

                a := 1;

                writeln (infile, a);                               (* step 3: put the integer into the file *)

                close (infile)                                         (* step 4: close the file properly *)

end;

 

Relevant past paper:

DSE ICT Elect B(SP-2017):  PP 4cd. 2012 4aii.

CE CIT Elect A(2005-2011): 2005 2a. 2009 2abi. 2010 4a. 2011 4cd.

No comments:

Post a Comment

Syllabus comparison

 Syllabus comparison   DSE ICT 2025 New syllabus DSE ICT 2012-2024 CE CIT 2005-2011 CE CS 1994-2004 ...