As well as understanding program code for file handling, pseudocode also helps designers create algorithms that are not specific to an actual programming language.
Here is a sample of pseudocode. Can you figure out what each file handling program does?
DECLARE count: INTEGER
count ← 0
OPENFILE "myText.txt" FOR READ
WHILE NOT EOF("myText.txt")
READFILE myText.txt, line
count ← count + 1
END WHILE
CLOSEFILE myText.txt
DECLARE msg:STRING
OPENFILE "myText.txt" FOR WRITE
INPUT msg
WHILE msg <> 0 DO
IF LENGTH(msg) > 5 THEN
WRITEFILE "myText.txt", msg
END IF
INPUT msg
END WHILE
CLOSEFILE "myText.txt"
OPENFILE "BOOK-FILE" FOR READ
IsFound ← FALSE
OUTPUT "Enter book"
INPUT ThisBook
REPEAT
READFILE "BOOK-FILE", FileBook
IF FileBook = ThisBook THEN
IsFound ← TRUE
OUTPUT "BOOK FOUND"
ENDIF
UNTIL IsFound = TRUE OR EOF("BOOK-FILE")
IF IsFound = FALSE THEN
OUTPUT "BOOK NOT FOUND"
ENDIF
CLOSEFILE "BOOK-FILE"
As you can see, there is not much to it. Here is a table comparing samples of pseudocode with program code.
| Python | Pseudocode |
|---|---|
| myfile = open("data.txt", "r") | OPENFILE myFile FOR READ |
| myfile = open("data.txt", "a") | OPENFILE myFile FOR APPEND |
| myfile.write("hello") | WRITEFILE myFile, "hello" |
| line = myFile.readline() | READFILE myFile, line |
| myfile.close() | CLOSEFILE myFile |