Library Functions:
C++ offers two categories of functions
that can be implemented in your program. 1. Library functions 2. User-defined
function. For now we will just focus on Library Functions. So, before we can
proceed further it is required to know about the terms Library
and Function.
Library:
A collection of sub programs
(functions) used to develop other programs and software is known as Library. C++ library of functions stores function
definitions in some categories e.g. standard input-output functions, string
function, I/O manipulator functions, mathematical function etc) under separate
files known as header files. Header files can be identified with .h extension
as a part of their file name.
Function:
A Function is a named set of
program instruction; designed to do certain task. A function can have list of
arguments, which represents the information being passed to the function and
also it can have return data type.
So,
by now we can say that - The functions whose definition of work or activity is
already been defined in the system is called Library Function. A
library function can be accessed by simply writing the function name, followed by a
list of arguments (depends on function prototype), which represent the
information being passed to the function.
Example:
char ch = getch( );//defined
in conio.h
Header File:
C++ standard library contains files containing function
prototypes, definitions, data types and constants for standard library
functions. These are known as header
files. These header files have .h extension as a part of their file name.
Example: stdio.h, string.h, math.h, stdlib.h, iostream.h,
iomanip.h etc.
Character I/O Functions:
So, far in our previous programs
and sample codes we have seen the use of cin and
cout these are the I/O operators
which gives you formatting control over the input and output, but these are not
character I/O functions. The functions which let’s you to read / write
character by character is called character
I/O function.
get( ):
The get() inputs a single character from the standard input device (by
default it is keyboard). Syntax:
device.get(char_variable); The
device can be any standard input device. If you want to get input from a
keyboard then you should use cin as
the device. Because, most of the computers consider the keyboard as the
standard input device, stdin.
Example:
char
ch;
cin.get(ch);
The get( )
function is a buffered input function. When you type in, data does not go into
your program unless you hit Enter key. As it is said earlier, get( ) function receives one character,
including white space, at a time from the input stream. Here you should
remember, an input stream can be a standard input stream object or user defined
stream object of the istream class.
Example:
//Sample
code illustrating the use of user-defined stream object with get()
ifstream
infile;
infile.open(“PARA.TXT”);
char
ch;
int
count=0;
while(infile)
{
infile.get(ch);/*get()is
used with object ‘infile’ of class ifstream,
reading character by character
from a disk file*/
if(isdigit(ch))
count++;
}
infile.close();
cout<<”\n
Total digits = “<<count;
put( ):
Syntax:
device.put(char_variable);
put( ) function sends one character at a time to the output
stream, where an output stream can be a standard output stream object or
user-defined output stream object.
Example:
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
#include<fstream.h>
void main()
{
clrscr();
char string[50];
cout<<"\n
Enter an string to write in a file";
gets(string);
fstream FILE;
FILE.open("MYTEXT.TXT",ios::app);
for(int i=0;string[i]!='\0';i++)
{
FILE.put(string[i]);/* writing one-by-one
character from
character array to a disk file named
as "MYTEXT.TXT"*/
}
FILE.close();
getch();
}
Output:
In the above program put() function is used with
user-defined output stream object (FILE) which represents a disk file “MYTEXT.TXT”.
Example 2:
char ch = ‘X’;
cout.put(ch); /*Writes
character ‘X’ on the standard output stream, sends output to stdout
(Standard
output device – Screen)*/
To use get( ) or put( )function
with user-defined input / output stream object, it is required to use “fstream.h”
header file in your program, otherwise including “iostream.h” is enough to use
cin / cout respectively.
Note**
More
discussion on get( ) and put( ) functions and using them with user-defined input/output
stream object will be discussed later. For now it is out of the scope of this
session.
In my next post I will write about some more character I/O
function – getchar(), putchar(), getch() and putch().
No comments:
Post a Comment