Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Field and Records

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 5

Field and records

A record is a data structure consisting of a fixed number of variables called fields. Every field that has an
identifier (name) and a data type. And then each field in a record can have a different data type.

Files

Many computers use a form of persistent data. This data is stored as secondary storage and will be used
whenever the application is run. Persistent data is stored in files, and some applications use databases,
and these databases are large complex file systems where all the low-level file organization is accessed
and managed by the user.

However, databases are not always useful for applications because sometimes a simpler file structure
can be used.

When working with files in a program you must be aware of any file handling constraints imposed by the
operating system, and you must also familiarize myself with the file handling feature in the chosen
programming languages. (most programming languages contain useful file handling functions).

Opening files

File management is the responsibility of the computer or the operating system. Most operating systems
require that the file must be opened before it can be read or modified, furthermore when you open a
file you must specify the name of the file but also the pathway if it is not in the same folder in where you
program code and the mode in which you want to open the file in.

It is also important to consider in which way you are opening the file as using the wrong mode can result
in loss or corruption of data.

Read mode is the go-to if you do not specify the mode when you open a file.

When trying to open a file, the operating system will check if the user has the necessary permission to
open it.

Some operating systems lock the file when it is opened to prevent any further modifications or even
delete it while working on it. Once you have officially finished working with a file you must close it so it
can be accessed by other users and if you forget to close the file you may find that some operations
performed are not saved.

File access modes

When opening a file, you must specify the access mode in which you will open the file in.

Read mode is default, and the following lists of modes are usually supported:
 Read: you can read from the file, but you cannot modify its contents because by default read
operations start from the beginning of the program.
 Write: you can write to the file but not read from it, the writing position will default to the start
of the file so any other existing data will be overwritten.
 Append: you can write from the file but not read from it.
 Read/write you can read and write to the file.

Closing files

With most operating systems it is essential to close the file as soon as you finish working with it.

This is particularly important with files that are used by more than one operating system as other users
may be locked out of the file while it is in use.

File handling errors

Because your file exists outside the program it is possible for someone or another program to delete or
move it or open it in a move that prevents you from accessing the file when needed.

Handling text files

A text file is made up of one or more lines of plain text. Text files are an excellent format because they
can easily read by humans which can then be helpful when identifying errors non text files are then
called binary files – they are files which can represent one or more complex data such as images or
computer games.

Text files can then be created, read or amended by computer programs. They can also be opened,
viewed and edited in a text editor.

File handling fundamentals

Before you can do anything with the file firstly you would have to open it, and when the operations are
complete the file must be then closed the operating system will then track the location of the files that
are open preventing other applications from editing it.

When opening a file in a text editor to check its contents the file will then be flagged by the operating
system as open. You must then close the file before attempting to write to it with your program or you
may get unexpected changes.

File handling modes

Most systems allow you to open files in different modes and this is designed to protect the file from
unexpected changes.
File handling functions

Most programming languages have a built-in library of functions that allow you to work with text files.
There may be other modules that can be imported for specialist file handling applications.

Creating a text file

Whilst it is possible to create a file using text editor software, it is also possible to create a new file
through program code. To create a text file, you must give the file a meaningful identifier and extension
which is .txt (this extension is commonly used for a general text file).

When you are creating a file using program code, unless you specify otherwise the file automatically
saves in a default folder. This is usually the same folder as the program code you have written to open
the file, meaning that you would not need to specify a file path.

Reading from a text file

When you are read from a text file, you can read the whole file in a single operation, or you can read the
file one-line at a time. If you decide to read the whole file at once you must make sure that there is
enough main memory to store the contents of the file. (It is important to remember that the amount of
free memory will depend on what other tasks are running at the same time).
To read the contents of the file you mut specify that it is open in read mode, as it only gives you
permission to only read from the file. And any attempts on writing on the file will result in an error.

Reading from a text file line by line

When reading line by line you must specify in the code the start and the end of where in the file you are
reading from and to.

When you want the file to read the whole of the file you would run a loop.

It is important that in most high-level languages you don't try and read beyond the text file.

However, in most high-level languages this is handled automatically.

Writing to a text file

When writing on a file you must specify that the file is opened in write mode “mode = w”

The open function would then open the file specifying the identifier name. And you should add the /n
string which specifies that this is a new line character and will not display any other character beyond
the line.

When you run the program, you can then open the file in the chosen text editor to make sure that you
have written the data to the file. Remember to also close the file when finished.

When you want to add further lines within the file you must then open the file in append mode.

Append mode// mode= a

File handling errors

Programs that handle files need very careful testing because a file has a life outside of your system.
Someone may delete the file, or move it, or the data in the file could be corrupted. This means that it is
essential that you catch any possible errors to prevent your system from crashing.

Most programming languages have built-in procedures to handle exceptions. In computer programming,
an exception is something that occurs while the program is running and, unless the exception is dealt
with, the program will crash. Many exceptions can be prevented by careful validation, but with file
handling there are a lot of things that can go wrong. You need to decide what will happen if the file
cannot be opened for some reason and write an appropriate exception handler.

Data encoding text files

Your text file is encoded as a sequence of characters. The default character coding for text files is usually
UTF-8, which is a form of Unicode and is the most common format across the world.
UTF-8 encoding has backward compatibility with ASCII. The first 128 characters in the Unicode character
set match those in the ASCII character set and UTF-8 encodes these characters into the same binary
strings as ASCII. This means that UTF-8 decoding can take a text file that is encoded as ASCII and convert
it to human-readable text without any compatibility issues.

You might also like