Another challenging concept in pseudocode can be writing to and reading text files. That is where we help. So you want to either write to or read from a text file that you have on your local computer? No problem! It is a lot easier than it seems, you are most likely wondering how to write to a file in pseudocode so let’s break it down.
Files can be both read and written to using pseudocode. This allows for very useful programs.
You should always add a close() statement after reading or writing to a text file.
A Guide to Files in Pseudocode
Reading files in Pseudocode
Reading a text file in pseudocode can be very helpful for extracting program output, getting user data and even inputs for a function or the entire program. We need a way to represent this in pseudocode, so we have a very simple way to read and write to text files in pseudocode.
To read from a simple text file in pseudocode we just use openRead. This is how we initialise the opening of a text file. You should ALWAYS assign this to a variable, for example: fileVariable = openRead(“pathToFile/file.txt”).
fileVariable = openRead("C:/pathToFile/file.txt") // assign openRead
We can then reference that variable when we want to read the file. We use fileVariable.ReadLine(). Inside of ReadLine we can denote a specific line number, which can be very helpful in For or While loops!
While i = 1 To 10 fileVariable.ReadLine(i) // reads line numbered i from the file EndWhile
To read to the end of a file in pseudocode we use the function endOfFile. So say we wanted to read our text file from before, we would say: fileVariable.readLine inside of a Do loop (see below). Then say we wanted to output that; we would just wrap it in an output: OUTPUT(fileVariable.readLine).
Do OUTPUT(fileVariable.readLine) // reads each line of the file until the file ends Until fileVariable.endOfFile
Writing to files in Pseudocode
This is just as simple as reading from files in pseudocode but is arguably much more useful. You can do things like take a user’s input and then store it in a file, or you could write your program output into a file. The possibilities are endless! This allows for very diverse and unique programs to be made, ones that are very useful. So how do you write to a file in Pseudocode?
To write to a text file you should use openWrite. Like with openRead, it is how we initialise the opening of a text file to then be written to. You should ALWAYS assign this to a variable, for example: fileVariable = openWrite(“pathToFile/file.txt”).
fileVariable = openWrite("pathToFile/file.txt") // declares the openWrite
Whenever we want to write to that file, we just reference fileVariable.
So say we wanted to write a user’s input to a text file, we would reference the variables when assigning the value to it. An example would be: fileVariable.writeLine(USERINPUT). It is really simple but can be extremely useful.
OUTPUT "Enter something to be written" fileVariable.writeLine(USERINPUT) // user input written to file
Closing files in Pseudocode
When you have finished writing or reading a text file, you should ALWAYS close it. This is best practice as it means that if you were to reference another file to write to later down the line, it will not override the previous file as it is closed!
fileVariable.close() // closes the openWrite