CAMBRIDGE AS Level Computer Science

FILE OPERATIONS

DIFFERENT FILE OPERATIONS

There are several types of file operations in pseudocode. These include:

PSEUDOCODE FOR FILE OPERATIONS:

Open a file named “Students.txt” in READ mode.
OPEN FILE "Students.txt" FOR READ
Opening a file named ‘Names.txt’ in WRITE mode.
OPEN FILE "Names.txt" FOR WRITE
Opening a file named Names.txt for Appending.
OPEN FILE "Names.txt" FOR APPEND
Reading a line of text from a file and store it into a string variable named 'text':
READ FILE "Names.txt", text

Example #1:

Write pseudocode for reading the content of the file named Namelist.txt and display the content on screen.
DECLARE MyFile : STRING DECLARE Line: STRING MyFile ← "Namelist.txt" // assigning filename to the string MyFile OPENFILE MyFile FOR READ // Opening the file for reading purpose WHILE NOT EOF(MyFile) DO // Reading lines by line from the file till end of file READFILE MyFile, Line // Reads a line from the file to Line OUTPUT Line // Outputting the content of Line ENDWHILE CLOSEFILE MyFile

Example #2:

Write pseudocode to transfer all the content of the file “Candidates.txt” to “Finalist.txt”
DECLARE Source: STRING DECLARE Target: STRING DECLARE Line: STRING Source ← "Candidates.txt" Target ← "Finalist.txt" OPENFILE Source FOR READ OPENFILE Target FOR WRITE WHILE NOT EOF(Source) DO READFILE Source, Line WRITEFIILE Target, Line ENDWHILE CLOSEFILE Source CLOSEFILE Target

Example #3:

A file contains details of students. Each line stores details of one student like ID, Name, Class and Address. Write pseudocode to transfer the details of all the students who are from 12A to a new file called newrecord.txt. All fields are sorted as fixed-length fields as follows.
<ID> <Name> <Class> <Address>
ID – 4 characters
Name – 20 characters
Class – 3 characters
Address – 3 characters
(Total number of characters a line takes is 30)
Solution:
DECLARE SourceFile : STRING DECLARE TargetFile : STRING DECLARE Line, Class : STRING SourceFile="Students.txt" TargetFile="NewFile.txt" OPENFILE SourceFile FOR READ OPENFILE TargetFile FOR WRITE WHILE NOT EOF(SourceFile) READFILE SourceFile, Line Class ← MID(Line, 20, 3) IF Class="12A" THEN WRITEFILE TargetFile, Line ENDIF ENDWHILE CLOSEFILE SourceFile CLOSEFILE TargetFile

Get In Touch

123 Street, New York, USA

info@ictmadesimple.com

+012 345 67890

Newsletter

Follow Us

© www.ictmadesimple.com. All Rights Reserved.