Text Files

Last Updated on

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.

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.

Code Icon

Basics:

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
Code Icon

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!