KEMBAR78
File Handling | PDF
0% found this document useful (0 votes)
15 views2 pages

File Handling

The document provides examples of file handling operations including reading from, writing to, and appending to files in a programming context. It also includes a task to output the last three lines of a file named 'Employee.txt' and demonstrates how to extract specific information based on a condition. The code snippets illustrate the use of loops and string manipulation to achieve these tasks.

Uploaded by

hussain tariq
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views2 pages

File Handling

The document provides examples of file handling operations including reading from, writing to, and appending to files in a programming context. It also includes a task to output the last three lines of a file named 'Employee.txt' and demonstrates how to extract specific information based on a condition. The code snippets illustrate the use of loops and string manipulation to achieve these tasks.

Uploaded by

hussain tariq
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

Wednesday, 15 November 2023 2:14 pm

File Handling

Read from a file:

DECLARE MyString : STRING

OPENFILE "Employee.txt" FOR READ


READFILE "Employee.txt", MyString
CLOSEFILE "Employee.txt"

Write to a file:

DECLARE MyString : STRING


MyString <- "Hello World"
OPENFILE "abc.txt" FOR WRITE
WRITEFILE "abc.txt", MyString
CLOSEFILE "abc.txt"

Append to a file:

DECLARE MyString : STRING


MyString <- "Hello World"
OPENFILE "abc.txt" FOR APPEND
WRITEFILE "abc.txt", MyString
CLOSEFILE "abc.txt"

OPENFILE "Employee.txt" FOR READ


WHILE NOT EOF("Employee.txt") DO
READFILE "Employee.txt", MyString
OUTPUT MyString
ENDWHILE
CLOSEFILE "Employee.txt"

Task) Write a program to output last three lines of file "Employee.txt". There are at least three
lines in the text file.

DECLARE Count, LineNumber : INTEGER


DECLARE MyString : STRING

Count <- 0

OPENFILE "Employee.txt" FOR READ


WHILE NOT EOF("Employee.txt") DO
READFILE "Employee.txt", MyString
Count <- Count + 1
ENDWHILE
CLOSEFILE "Employee.txt"
New Section 1 Page 1
CLOSEFILE "Employee.txt"

OPENFILE "Employee.txt" FOR READ


FOR LineNumber <- 1 TO Count - 3
READFILE "Employee.txt", MyString
NEXT LineNumber

FOR LineNumber <- 1 TO 3


READFILE "Employee.txt", MyString
OUTPUT MyString
NEXT LineNumber

CLOSEFILE "Employee.txt"

OPENFILE "Employee.txt" FOR READ


WHILE NOT EOF("Employee.txt") DO
READFILE "Employee.txt", MyString
IF LEFT(MyString,4) = "2678" THEN
OUTPUT "Name:" & MID(MyString,5,10)
OUTPUT "Age:" & RIGHT(MyString,2)
ENDIF
ENDWHILE
CLOSE "Employee.txt"

New Section 1 Page 2

You might also like