C++ getline() and write() Functions

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:

char 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:

cin >> name;

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()
#include < iostream>
using namespace std;
int main()
{
int size = 20;
char 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";
return 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:

cout.write (line, size)

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()
#include <iostream >
#include <string.h>
using namespace std;
int main()
{
int i; char * string1 = "c++ ";
char * string2 = "Programming";
int m = strlen(string1);
int n = strlen(string2);
for (int i=1; i < n; i++)
{
cout.write(string2,i);
cout << "\n";
}
for (i=n; i>0; i--)
{
cout.write(string2, i);
cout << "\n";
}
// concatenating strings
cout.write(string1,m) . write(string2,n);
cout << "\n";
// crossing the boundary
cout.write(string1,10);
return 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);

  • C++ Dynamic Initialization of Objects
  • C++ Copy Constructor
  • C++ Dynamic Constructor
  • C++ Destructors
  • C++ Exercise
  • C++ Operator Overloading
  • C++ Overloading Unary Operators
  • Const pointer in C
  • Void pointer in c
  • C++ Overloading Binary Operators
  • C++ Overloading Binary Operators Using Friends
  • C++ Manipulation String Using Operators
  • C++ Rules for overloading operators
  • C++ Exercise
  • C++ Basic To class Type
  • C++ Class TO Basic Type
  • C++ One class To another class type
  • C++ Exercise
  • C++ Inheritance introduction
  • C++ Single Inheritance
  • C++ Multiple Inheritance
  • C++ Ambiguity Resolution in inheritance
  • C++ Hierarchical Inheritance
  • C++ Hybrid Inheritance
  • C++ Virtual Base Classes
  • C++ Exercise
  • C++ abstract class
  • C++ nesting of classes
  • C++ Exercise
  • C++ polymorphism
  • C++ Exercise
  • C++ pointers
  • C++ Pointers TO object
  • C++ this pointer
  • C++ Pointer to Derived class
  • C++ Virtual functions
  • C++ Exercise
  • C++ streams
  • C++ unformatted I/O operations
  • C++ Put() and get()
  • C++ getline() and write()
  • C++ Formatted console I/O
  • C++ Manipulators
  • C++ Exercise
  • C++ file handling
  • C++ file stream classes
  • C++ Open and closing file
  • C++ open using constructor
  • C++ open using open()
  • C++ Detecting End of file
  • C++ File modes
  • C++ File pointers and Manipulators
  • C++ Sequential I/O
  • C++ Reading and Writing
  • C++ Updating a File
  • C++ Error handling In File
  • C++ Command Line Arguments
  • C++ Exercise
  • C++ Template introduction
  • C++ Class Templates with multiple Parameters
  • C++ Function templates
  • C++ Function templates with multiple parameters
  • C++ member function Template
  • C++ Exercise
  • C++ Exception handling
  • C++ Basics of Exception Handling
  • C++ Exception Handling Mechanism
  • C++ Throwing Mechanism
  • C++ Catch Mechanism
  • C++ Catch all Exceptions
  • C++ Re-Throwing An Exception
  • C++ Specifying Exceptions
  • C++ Exercise