Tuesday, August 28, 2018

Managing Data Through Files


The files allow the data to be physically stored on the permanent storage device. Many devices require that information be written to or read from an auxiliary storage [disk: pen drive, hard disk, DVDs] device. Such information is stored on the device in the form of a file. The characteristics of files are:

  • File contents are stored in a permanent storage medium [disk, drum or tape].
  • File data can be of different data types.
  • Files can be arbitrary and of unlimited size.

Organization of files

Files classified into two types, based on the mode of access.

  1. Sequential: in a sequential file all the components are stored in a sequential order. Creating sequential file is easier but accessing file information is slow. In a magnetic tape, only sequential files can be stored.
  2. Random access or direct access: Random access files are those files in which the file information accessed at random. The random files are faster to operate and maintenance of these files easier.

The C language supports both types of files.

Declaring a File

You create a file as a pointer to the file's structure in memory. When you write information to the file, or read information from it, your program gets the information it needs from the structure. To declare a file, use this syntax:

FILE *file_pointer;

FILE *infile , *outfile ;

Here FILE is a constant structure name defined in <stdio.h> file and to deal with file you have to create a pointer variable of this type.

Opening a File

The fopen() function opens the file in user defined mode and passes open file's structure address to file pointer for further processing. This function actually creates link between storage device, computer and the program. The operating system uses FILENAME for file identification and program uses file_pointer for processing the particular file. The fopen() function creates this link and passes control to program for further processing.

file_pointer = fopen ( “FILENAME.Ext” , “mode” );

infile = fopen ( “student.dat” , “w” );   //open file for write operations.

The file can be opened for read only, write only or both, open for append only, create/not create if does not exists and overwrite if exists. These all operations are mentioned in fopen() function using mode type parameter. The following table shows all modes:

Mode

Description

“w”

Open for write operation. If file exist, it deleted and new file created.

“r”

Open for read operation. If file does not exist, NULL/error code returned.

“a”

Open for append operation. New data add at the end of file. If file does not exist, NULL/error code returned.

“r+”

Open existing file for updating.

“w+”

Create new file for read and write operation.

“a+”

Open for append operation. Create file if it does not exists.

Closing a File

To disconnect the link created by fopen() or to close the file C language offers one function called fclose(). This function takes file pointer as its parameter and closes it to prevent any unintentional damage caused by other program activities. 

fclose (file_pointer );

fclose (infile) ; fclose (outfile);

File Operative Functions

The C language provides many library functions to deal with all sort of file operations, viz., read/writing unformatted/formatted character or string or structure. The below table shows such functions:

 Function

Description

putc() , fputc()

To write data to a file/printer, a character at a time.

getc() , fgetc()

To read data from a file/printer, a character at a time.

puts() , fputs()

To write data to a file/printer, a string at a time.

gets() , fgets()

To read data from a file/printer, a string at a time.

 fprintf()

To write formatted data to a file/printer.

fscanf()

To read formatted data from a file/printer.

 fwrite()

To write entire structure to a file/printer.

fread()

To read entire structure from a file/printer.

 


Previous Page Main Contents  

No comments:

Post a Comment