getline c, get c, put c and write c – Unformatted stream I/O functions
- getline c, get c, put c and write c – These functions are called unformatted stream IO functions where
- get() and put() can handle single character input and output and
- getline() and write() can handle multi character input and output.
- Header files for these above functions : “iostream.h”
Single character functions : get() and put()
Single Character functions are used for reading and displaying a single character respectively. These functions are :
- get() :
- The get() function is an input function.
- It fetches a single character and store it in a character variable.
- The get() is a member function of istream class and it is involved with an istream object cin.
char ch; cin.get(ch); // get a character from keyboard and assign it to ch.
- put() :
- The function put() is an output function and is used to output a character at a time.
- The put() function is also a member of ostream class and is involved with an ostream object cout.
Example of get() and put() :
char ch; while (cin.get(ch)!='\n') cout<<ch;
char ch; cin.get(ch); cout.put(ch); cout.put(65)
Multi Character Functions : getline() and write()
The single character functions defined above can handle single character at a time and cannot handle multiple characters. To solve this problem, there are functions that can handle strings also. These functions are :
- getline() –
- The getline() is an input function and needs a line of text that ends with a newline character.
- This function is used as shown below :
- where line specifies the name of the variable that will store the line of text, and
- size specifies that maximum size-1 characters can be stored in line;
- 1 extra character in size is reserved for null character ‘\0’.
- write() –
- The function write() is an output function and can display an entire line.
- It takes the following form :
- where line specifies the string variable whose contents are to be displayed, and
- size specifies the number of characters to be displayes.
Example of getline() and write() :
char name; cin.getline(name,30);
Incoming search terms:
- getline and write function in c
- to display a single character from a specified file on the screen?
- write a program for get and put function