Thursday 5 December 2019

C Tutorial - Data File Handling through C

Data File Handling through C

Introduction

Many applications require that information be written to or read from an auxiliary memory.

Such information is stored on the memory device in the form of a data file.

Thus files allow us to store information permanently, and to access and alter that information whenever necessary.

In C, an extensive set of library; functions is available for creating and processing data files. Unlike other files.

However, there are two different types of data files

1. Stream-oriented (or standard) data files:

Stream oriented data files are generally easier to work with and are therefore more commonly used.

Stream-oriented data files can be subdivided into two categories.

The first category are text files, consisting of consecutive characters. There characters can be interpreted as individual data items, or as components of strings or numbers.

2. System-oriented (or low level) data files:

System-oriented data files, often referred to as unformatted data files, organizes structures, such as arrays and structures.

A separate set of library functions is available for processing stream-oriented data files of this type.

These library functions provide single instructions that can transfer entire arrays or structures to or from data files.

Note: Library function has been discussed in detail in next section.



File operation

There are two distinct ways to perform the file operation in C:

1. Low level I/O operation (it uses UNIX system call therefore we won't discuss here)

2. High level I/O operation (it uses function in C's Standard I/O library)

List of I/O function with their operation:

fopen()
creates a new file for use or opens an existing file for use.
fclose()
close a file which has been opened for use.
getc()
reads a character to a file.
putc()
writes a character to a line.
fprintf()
writes a set of data values to a file.
fscanf()
reads a set of data values from a file.
getw()
reads an integer from a file.
putw()
writes an integer to a file.
feof()
test for an end of file condition.

Opening & closing a data file

When working with a stream-oriented data file, the first step is to establish a buffer area (a holt station for data processing) where information is temporarily stored while being transferred between the computer's memory and the data file.

This buffer area allows information to be read from or written to the data file more readily than would otherwise be possible. The buffer area is established by writing

FILE * ptvar;

Where FILE (uppercase letter required) is a special structure type that establishes the buffer area, and ptvar is a pointer variable that indicates the beginning of the buffer area.

The structure type FILE is defined within a system include file, typically stdio.h. The pointer ptvaris often referred to as a stream pointer, or simply a stream.

A data file must be opened before it can be created or processed. This associates the file name with the buffer area (i.e., with the stream).

It also specifies how the data file will be utilized, i.e., as a read-only file, a write -only file, or a read/write file, in which both operations are permitted.

The library function open is used to open a file. This function is typically written as Ptvar = open (file-name, file-type)

Where file-name and file-type are strings that represent the name of the data file and the manner in which the data file will be utilized.

The name chosen for the file-name must be consistent with the rules for naming files, as determined by the computer's operating system.

The file-type must be one of the strings shown:

"r"
Open an existing file for reading only.
"w"
Open a new file for writing only. If a file with the specified filename currently exists, it will be destroyed and a new file will be created in its place.
"a"
Open an existing file for appending. A new file will be created if the file with the specified file-name don't exists.
"r+"
open an existing file for both reading and writing
"w+"
Open an existing file for both reading and writing. If a file with the specified file name currently exists, it will be destroyed and a new file created in its place
"a+"
Open an existing file for both reading and appending. A new file will be created if the file with the specified file-name does not exists.

Creating a data file

data file must be created before it can be processed.

stream-oriented data file can be created in two ways. One is to create the file directly, using atext editor or a word processor.

The other is to write a program that enters information into the computer and than writes it out to the data file.

Unformatted data files can only be created with such specially written programs.

Example:

Reading a data file-

The following program will read a line of text from a data file on a character by character basis, and display the text on the screen.

The program makes use of the library functions get and putchar to read and display the data.

Data file consisting entirely of strings can often be created and read more casually with programs that utilize special string-oriented library functions.

Some commonly used functions of this type are gets, puts, fgets and fputs.

The functions gets and puts read or write strings to or from the standard output devices, whereas fgets and fputs exchange strings with data files.


Processing a data file

Most data file applications require that a data file be altered as it is being processed.

For example, in an application involving the processing of customer records, it may be desirable to add new records to the file there requirements in turn suggest several different computational strategies.

Another approach is to work with two different data files- an old file and a new file. Each record is read from the old file, updated as necessary, and then written to the new file.

When all of the records have been updated, the old file is deleted or placed into archival storage and the new file renamed. Hence, the new file become the struck for the next round of updates.

Historically, the origin of this method goes back to the early days of computing, when data files were maintained on magnetic tapes. The method is still used, however, because it provides a series of old source.

File that can be used to generate a customer history. The most recent source file can also be used to recreate the current file if the current file is damaged or destroyed.

Unformatted data file

Some applications involve the use of data files to store block of data, where each block consists of a fixed number of contiguous bytes.

Each block will generally represent a complex data structure, such as a structure or an array.

For example, a data file may consist of multiple structures having the same composition, or it may contain multiple arrays of the same type and size.

For such applicators it may be desirable to read the entire block from the data, or write the entire block to the data file, rather than reading or writing the individual components (i.e., structure members of array elements) within each block separately.

The library functions fread and fwrite are intended to be used in situations of this type.

There functions are often referred to as unformatted read and write functions. Similarly, data files of this type are often referred to as unformatted data file.

Each of these functions requires four arguments: a pointer to the data block, the size of the data block, the number of data blocks being transferred, and the stream pointer.

Thus, a typical fwrite function might be written as: fwrite(&customer, sizeof(record), 1, fpt);

Where customer is a structure variable of type record, and fpt is the stream pointer associated with a data file that has been opened for output.


Previous Page                                       Next Page

More Topics:













100+ Best Home Decoration Ideas For Christmas Day 2019 To Make Home Beautiful

Best gifts for Christmas Day | Greeting cards for Christmas Day | Gift your children a new gift on Christmas day This Christmas d...