Part E - Secondary Storage
Records and Fields
Stream data using standard library functions to access persistent text
"Data organization involves fields, records, files and so on. A
data field holds a single fact or attribute of an entity"
(Laudon, 2007)
Records |
Fields |
Tables |
Exercises
Persistent data hierarchies typically consist of databases composed of
files, which consist of records, which consist of fields, which consist
of bytes, which are stored in bits. In files that hold a tabular
structure, each record contains the same number of fields.
This chapter describes how to identify records and fields in a text
file and how to retrieve tabular data.
Records
A record occupies a single line in a text file and holds all of the
data associated with one chunk of information.
The record is a sequence of characters that ends with
a record delimiter. The typical record delimiter is
the newline character (\n).

Consider a text file named produce.txt
containing information about items of produce in a grocery store.
Each record consists of the sku for a product and its unit price.
4664 1.49
4419 1.29
4011 0.59 |
To determine the number of records in this file,
we count the number of newline characters:
// Number of Records
// records.c
#include <stdio.h>
int main(void)
{
FILE *fp = NULL;
int c, nrecs;
fp = fopen("produce.txt", "r");
if (fp != NULL) {
nrecs = 0;
do {
c = fgetc(fp);
if (c != EOF) {
if ((char)c == '\n')
nrecs++;
}
} while (feof(fp) == 0);
printf("%d records on file\n",
nrecs);
fclose(fp);
}
return 0;
} |
3 records on file
|
Since this program determines the number of records in the file by counting
the newline characters,
to report the correct number of records, the last record in the
file must end with a newline character. If the last record does not
end with a newline character, the count will be off by
one.
Fields
A field holds one element of information within a single record.
We separate adjacent fields within a record by a field delimiter.

Consider the file named produce.txt (see above).
Each record contains two fields: the first field
holds the sku and the second field holds the unit price.
The field delimiter is a blank character.
The following program reads the fields of each record in the file and
displays their contents
// Record and Fields
// recordFields.c
#include <stdio.h>
int main(void)
{
FILE *fp = NULL;
int sku;
double price;
fp = fopen("produce.txt", "r");
if (fp != NULL) {
printf(" Produce Items\n"
" =============\n\n"
"sku Price\n"
"---------------\n");
while (fscanf(fp,"%d%lf\n",
&sku, &price) == 2)
printf("%4d %10.2lf\n",
sku, price);
fclose(fp);
}
return 0;
} |
Produce Items
=============
sku Price
---------------
4664 1.49
4419 1.29
4011 0.59
|
Tables
A table is a set of records in which each record contains the same
number of fields.

If one of the fields in a record is a character field, the blank character
might not be suitable as a field delimiter and we select a special character
for that purpose.
Consider the file named sale.txt; its
contents are listed below.
Each record in this file contains three fields: the first field
holds the sku, the second field holds a character describing
the price status (an * denotes a sale, a blank character denotes
a regular price) and the third field holds the unit price.
The field delimiter is the semi-colon character:
4664;*;1.39
4419;*;1.09
4011; ;0.59 |
The following program reads each record from the file
and displays the fields in a tabular format
// Tabular Data
// table.c
#include <stdio.h>
int main(void)
{
FILE *fp = NULL;
int sku;
char status;
double price;
fp = fopen("sale.txt","r");
if (fp != NULL) {
printf(" Produce Items\n"
" =============\n\n"
"sku Sale Price\n"
"---------------\n");
while (fscanf(fp, "%d;%c;%lf",
&sku, &status, &price) == 3)
printf("%4d %c %8.2lf\n",
sku, status, price);
fclose(fp);
}
return 0;
} |
Produce Items
=============
sku Sale Price
---------------
4664 * 1.49
4419 * 1.29
4011 0.59
|
Note that we have included the field delimiters within fscanf()'s
format string.
Exercises
- Complete the exercise on Tables
- Complete the following practice problems
|