Text Files in Pseudocode
Another challenging concept in pseudocode can be reading and writing to 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.
With files you can read to and also write to them in pseudocode granted that the file is on your device. If it is then you can easily do this, it is a lot simpler than you most likely think! Also, if you are a programmer that uses high-level languages such as Python, JavaScript, or .NET languages then you will most likely know how to do this in that language.
We follow the pseudocode standard set by the main computer science exam board in the UK, AQA, allowing a universal set of functions and operators to be used. This makes it much easier for programmers to understand each other’s code without comments!
Basics:
- 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.
How to Use Files in Pseudocode
Files can store and maintain data even after a program is closed, making them very useful for data handling across multiple sessions.
Reading from Files
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").
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!
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).
Writing to Files
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").
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.
Closing Files
When you have finished writing or reading a text file, it is imperative that you 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!
Try our pseudocode editor.
Give our pseudocode editor a go today for free - with a built-in compiler, tools to convert pseudocode to code, and project saving, PseudoEditor makes writing pseudocode easier than ever!
So there you go, you now know exactly how to read, write to and close files in pseudocode! Have a go on our online pseudocode editor today, also try reading up on variables in pseudocode!