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