C++ basic_ios Library - getline



Description

It is used to extracts characters from the stream as unformatted input and stores them into s as a c-string, until either the extracted character is the delimiting character, or n characters have been written to s (including the terminating null character).

Declaration

Following is the declaration for std::basic_istream::getline.

basic_istream& getline (char_type* s, streamsize n );
basic_istream& getline (char_type* s, streamsize n, char_type delim);

Parameters

  • s − Pointer to an array of characters where extracted characters are stored as a c-string.

  • n − Maximum number of characters to write to s (including the terminating null character).

  • delim − Explicit delimiting character: The operation of extracting successive characters stops as soon as the next character to extract compares equal to this (using traits_type::eq).

Return Value

Returns the basic_istream object (*this).

Exceptions

Basic guarantee − if an exception is thrown, the object is in a valid state.

Data races

Modifies the elements in the array pointed by s and the stream object.

Example

In below example for std::basic_istream::getline.

#include <iostream>

int main () {
   char name[256], title[256];

   std::cout << "Please, enter your name: ";
   std::cin.getline (name,256);

   std::cout << "Please, enter your favourite movie: ";
   std::cin.getline (title,256);

   std::cout << name << "'s favourite movie is " << title;

   return 0;
}

The output should be like this −

Please, enter your name: tutorialspoint
Please, enter your favourite movie: ted
tutorialspoint's favourite movie is ted 
istream.htm
Advertisements