File reading lets a program bring stored information into memory, where it can be held in variables or a buffer and used by an algorithm. Learn how the Readfile command works, and how it differs from writing data or closing a file—small steps that make file handling clear.

Multiple Choice

Which operation would you use to read data from a file into the program's memory?

Reading data from storage into the program’s memory is done with a read operation that fetches the file’s contents so you can work with them in memory. The Readfile command does exactly that: it reads the data from the file and loads it into variables or a buffer in your program. The other options don’t bring data into memory: writing would push memory data out to the file, closing ends access to the file but doesn’t transfer data, and ending file use stops the session without reading anything.

Reading Data from a File: How Programs Bring Information into Memory

A program can only work with information it can access. When that information is stored in a file, the program needs a way to bring it into memory. In pseudocode, a command such as READFILE does this job: it takes data from a file and makes it available for the program to use.

That may sound simple, but file reading is part of a larger process. The program must identify the file, open it, read its contents in a suitable way, and close it when it’s finished. Think of a file like a book in a library. Opening the book gives you access; reading a page brings information to you; closing it ends the session.

What does READFILE do?

A read operation transfers data from a file into the program’s working memory. The program can then store the data in variables, display it, compare it, or use it in calculations.

For example, a file might contain a student’s name and score:


Mina

82

A program could read those values and store them in variables:


OPENFILE "scores.txt" FOR READ

READFILE "scores.txt", StudentName

READFILE "scores.txt", Score

CLOSEFILE "scores.txt"

After the first read, StudentName contains "Mina". After the second, Score contains 82. The file still exists on storage; reading it doesn’t remove the data. It simply copies information into memory, where the program can work with it.

Different pseudocode styles may write file commands in slightly different ways. Some use a file name with every read operation. Others open a file once, give it a handle or identifier, and then read from that open file. The exact wording can vary, but the purpose is the same: retrieve data from storage.

Reading is not writing

Reading and writing move information in opposite directions.

When a program reads a file, information moves from the file into memory. When it writes to a file, information moves from memory into the file. A useful way to remember the difference is to picture a notebook. Reading means taking information out of the notebook so you can use it. Writing means putting new information into it.

A write command won’t load stored data into program variables. A close command won’t transfer data either; it simply ends the program’s access to the file. These operations have separate roles, and mixing them up can lead to confusing results. It’s a bit like confusing “save” with “open”—both involve a document, but they do very different things.

The usual steps for working with a file

File handling often follows a clear sequence:

  1. Open the file in the mode the program needs, such as reading.

  2. Read data from the file into variables or another memory area.

  3. Process the data, perhaps by displaying it or calculating a total.

  4. Close the file when it’s no longer needed.

Here’s a short example using a file that contains one temperature:


DECLARE Temperature : REAL

OPENFILE "weather.txt" FOR READ

READFILE "weather.txt", Temperature

CLOSEFILE "weather.txt"

OUTPUT Temperature

The program opens the file for reading, transfers a value into Temperature, then closes the file. The output command can now display the value held in memory.

Closing the file matters, even though it doesn’t read or write data. It tells the system that the program has finished with that file. Keeping files open longer than necessary can waste resources or cause problems if another part of a system needs access.

Files often contain more than one value

A program may need to read several records rather than just one. A file containing a list of names, for instance, could have one name on each line. The program can read each name in turn, handling it before moving to the next.


OPENFILE "names.txt" FOR READ

READFILE "names.txt", Name

OUTPUT Name

READFILE "names.txt", Name

OUTPUT Name

CLOSEFILE "names.txt"

This small example reads two entries. Real programs often use a loop to handle a list of unknown length:


OPENFILE "names.txt" FOR READ

WHILE NOT EOF("names.txt")

READFILE "names.txt", Name

OUTPUT Name

ENDWHILE

CLOSEFILE "names.txt"

EOF means “end of file.” It helps the program know when there’s no more data to read. Without a check like this, the program might try to read beyond the last record. That’s similar to turning a page in a book after you’ve reached the back cover: there’s simply nothing there.

The exact order of an end-of-file check can depend on the pseudocode rules being used. Some systems check before reading; others read and then check. What matters is that the program has a safe way to stop once the data runs out.

Where does the data go?

A read command usually places data into a variable, or into a buffer that holds information temporarily. A variable gives the program a named place to keep a value. For example:


READFILE "scores.txt", Score

Here, the value read from the file is stored in Score. The program can then use that variable:


IF Score >= 50 THEN

OUTPUT "Pass"

ELSE

OUTPUT "Try again"

ENDIF

The file provides the original data, while the variable gives the program a way to refer to that data. This distinction is useful: a file is long-term storage, while memory holds information the program is using now. When the program stops, values held only in memory may disappear; values saved in a file can remain available later.

A few details worth keeping in mind

Data in a file must be read in a form the program can understand. If a file stores a number as text, the program may need to convert it before doing arithmetic. For example, "82" can look like a number to a person, but it may be treated as a string until the program changes its type.

The order of the data matters too. If a program expects a name followed by a score, but the file contains the score first, the values may end up in the wrong variables. A file’s layout is a kind of agreement between the program and the data it reads.

It’s also important to handle missing or empty files sensibly. A file might not exist, or it might contain no records. Good algorithms account for these possibilities rather than assuming every file is perfect.

The central idea

A READFILE operation brings information from a stored file into program memory. Once the data is in memory, the program can display it, check it, calculate with it, or use it in other decisions. Opening a file allows access; reading transfers data; closing ends that access. Each step has its own job.

That distinction is the heart of file handling. Reading brings information in. Writing sends information out. Closing simply finishes the connection.