C++ getline() and write() Functions
We can read and displays a line of text more efficiently using the line-oriented input/output functions getline() and write(). The getline() function reads a whole line of text(strings) that and ends with a newline character(transmitted by RETURN key). This function can be invoked by the help of object cin as follows:
cin.getline (line, size);
|
This function call invokes the function getline() which reads the character input into the variable line.
The reading is terminated as soon as whenever the newline character ‘\n’ is encountered or size-1 characters are read (whichever occurs first). The newline character is read but it is not saved. Instead, it is
replaced by the null character. consider the following example:
name[20];
cin.getline(name,20);
|
Assume that we have given the following input using the keyboard:
Bjarne Stroustrup < press return press >
in the above example This input will be read correctly and assigned to the character array name. for example input is as
follows:
Object Oriented Programming < press RETURN >
in the above example In this case, the input will be terminated after reading the following 19 characters:
Object Oriented Pro
Note that the two blank spaces contained in the string are also taken into account. We can also read strings using the operator>> as follows:
But note that cin operator can read strings that do not contain white spaces. This means that cin operator can read just one word and not a series of words such as “Bjarne Stroustrup”. But it can read the following
string correctly:
After reading the string, cin operator automatically adds the terminating null character to character array.
in the below program demonstrates the use of >> and getline() for reading the strings.
READING STRINGS WITH getline()
< iostream>
using namespace std;
main()
{
size = 20;
city[20];
cout << "Enter city name: \n";
cin >> city;
cout << "City name:" << city << "\n\n";
cout << "Enter city name again: \n";
cin.getline(city, size);
cout << "City name now: " << city << "\n\n";
cout << "Enter another city name: \n";
cin.getline(city, size);
cout << "New city name: " << city << "\n\n";
0;
}
|
The output of Program 10.2 would be:
First run
Enter city name:
Delhi
City name: Delhi
Enter city name again:
City name now:
Enter another city name:
Chennai
New city name: Chennai
Second run
Enter city name:
New Delhi
City name: New
Enter city name again:
City name now: Delhi
Enter another city name:
Greater Bombay.
New city name: Greater Bombay
|
Note: During first run, the newline character ‘\n’ at the end of “Delhi which is waiting in the input queue is read by the getline() that follows immediately and therefore it does not wait for any response to the prompt ‘Enter city name again:’. The character ‘\n’ is read as an empty line. During the second run, the word “Delhi” (that was not read by cin) is read by the function getline() and, therefore, here again it does not wait for any input to the prompt ‘Enter city name again:’. Note that the line of text “Greater Bombay” is correctly read by the second cin.getline(city,size); statement.
The write() function displays an entire line and has the following form:
The first argument line presents the name of the string to be displayed and the second argument size indicates the number of characters to display.
Note that it does not stop displaying the characters automatically when the null character is encountered. If the size is greater than the
length of line, then it displays beyond the bounds of line.
Program illustrates how write() method displays a string.
DISPLAYING STRINGS WITH write()
<iostream >
<string.h>
using namespace std;
main()
{
i;
* string1 = "c++ ";
* string2 = "Programming";
m = strlen(string1);
n = strlen(string2);
(int i=1; i < n; i++)
{
cout.write(string2,i);
cout << "\n";
}
(i=n; i>0; i--)
{
cout.write(string2, i);
cout << "\n";
}
cout.write(string1,m) . write(string2,n);
cout << "\n";
cout.write(string1,10);
0;
}
|
Look at the output of Program
P
Pr
Pro
Prog
Progr
Progra
Program
Programm
Programmi
Programmin
Programming
Programmin
Programmi
programm
program
program
progra
progr
prog
pro
pr
p
|
c++ Programming
c++ Progr
The last line of the output indicates that the statement
cout.write(string1, 10);
displays more characters than what is contained in string1. It is possible to concatenate two strings using the write() function. The statement
cout.write(string1, m), write(string2, n);
is equivalent to the following two statements:
cout.write(string1, m);
cout.write(string2, n);