What are the Various Input/Output Functions used in Files?


In C language the input, output operations with files are performed with the help of library functions. Once a file is opened, reading out of or writing to it is accomplished using the standard I/O routines.

The getc and putc functions:

The simplest file I/O fns are getc and putc. Assume that a file is opened with mode w and file pointer fp. Then the statement:

putc ( c, fp );

Writes the character contained in the character variable C to the file associated with FILE pointer fp. Similarly, getc is used to read a character from a file that has been opened in read mode.

Ex: c = getc ( fp2);

Would read a character from the file whose file pointer is fp2.

The file pointer moves by one character position for every operation of getc or putc.
The getw and putw functions.

The getw and putw are integer –oriented functions. They are similar to the getc and putc functions and are used to read and write integer values. These functions would be useful when we deal with only integer data. The general forms of gete and putw are :

putw ( integer, fp);

getw (fp);

The fprintf and fscanf functions:

fprintf and fscanf can handle a group of mixed data simultaneously.

The functions fprintf and fscanf perform I/O operations that are identical to the familiar printf and scanf functions, except of course that they work on files.

The first argument of these functions is a file pointer which specifies the file to be used .The general form of fprintf is

fprintf ( fp, “control string”, list);

Where fp is a file pointer associated with a file that has been opened for writing. The control string contains output specifications for the items in the list. The list may include variables, constants and strings.

Ex: fprintf ( f1, “%s%d%f”, name , age, 7.5);

Here, name is an array variable of type char and age is an int variable.

fprintf ( file pointer, “control string”, list of arguments);

The general format of fscanf is:

fscanf ( fp; “control string”, list);

This statement would cause the reading of the items in the list form the file specified by fp, according to the specifications contained in the control string.

Syntax: fscanf ( file pointer, “control string”, list of address of arguments);

Ex: fscaf ( f2, “%s%d, item, & quantity);

Like scanf, fscanf also returns the number of items that are successfully read.

fgets ( ):

This function is used to read a string from a text file.

Syntax: fgets ( string variable, integer, file pointer);

Ex: fgets ( line, 80, fp);

fputs ( ):

This function is used to write a string into a text file.

Syntax: fputs ( string data item, file pointer);

Ex: fputs ( name, fp);

I/O functions for binary files:

The library fns used to handle the I/O operations of binary files are fread, fwrite

fread:

This function is used to read unformatted data from a binary file into the memory.

Syntax: fread ( address of data elements number of bytes, number of blocks, file pointer);

Ex: int num;

fread ( &num, sizeOf ( int), 1, fp);

The above function fread reads a block of data of 2 bytes from the file pointed by fp and assigns that value to num from its address.

fwrite:

This function is used to write unformatted data from the memory to a binary files.

Syntax: fwrite (address of data elements, number of bytes, number of blocks, file pointer);

Ex: int num = 10;

fwrite ( &num, sizeOf ( int),1,fp);



Related

C Language 6164269703079472805

Post a Comment

emo-but-icon

item