Curriculum
Course: Learn C++
Login

Curriculum

Learn C++

Text lesson

I/O Stream Classes in C++

In this lesson, you will learn.

  • What is Stream
  • C++ Stream Classes
  • Input and Output Functions

 

What is Stream

  • A stream is a logical device that either produces or consumes information that is linked to a physical device(terminals, disk, tape drives etc.) by the I/O system.
  • The source stream provides data to program is called input stream and destination stream receives output from the program called output stream.

Stream Flow in C++

 

The C++ I/O Stream Classes

  • A stream is a general name given to a data flow and provides a convenient way to perform input and output operations.
  • In C++ a stream is represented by an object of a particular class like cin and cout stream objects for input and output.

 

The following figure shows the arrangement of the most important classess stream classes in C++.

 

1. The ios Class

The ios class is the granddaddy of all stream classes and contains most of the features needed to operate the C++ stream. The ios class is declared the virtual base class so that only one copy of its member is inherited by the iostream.

2. The istream and ostream

The istream and ostream classes are derived from ios and are dedicated to input and output operations, respectively

The istream class contains such functions as get(), getline(), read(), and the overloaded extraction (>>) operators, while ostream contains put() and write(), and the overloaded insertion (<<) operators.

3. The iostream class

The iostream class is derived from both istream and ostream by multiple inheritances. Classes derived from it can be used with devices, such as disk files, that may be simultaneously opened for both input and output.

Three classes—istream_withassign, ostream_withassign, and iostream_withassign—are inherited from istream, ostream, and iostream, respectively. They add assignment operators to these classes.

4. The streambuf class

Provides an interface to physical devices through buffers and acts as a base class for filebuf.

 

The get() and put() functions

The istream and ostream classes define the two functions i.e. get() and put() to handle single-character I/O operations.

Example 1: get() and put() Function for Character I/O

//StreamFunctionEx1.cpp
#include<iostream>
using namespace std;

int main(){
    int count=0;
    char c;

    cout<<"Enter text\n";
    cin.get(c);

    while(c!='\n'){
        cout.put(c);
        count++;
        cin.get(c);
    }

    cout<<"nTotal characters="<<count<<endl;
    return 0;
}

 

Output

Enter text
I like C++ and object oriented programming.
I like C++ and object oriented programming.
Total characters=43

 

In C++, getline() and write() are two functions commonly used for input and output operations.

1. getline() Function

The getline() function is used to read a line of text from an input stream (like std::cin or a file stream) into a std::string variable. It reads characters until it encounters a newline character ('\n') or the specified delimiter.

Syntax

std::getline(input_stream, str, delimiter);



  • input_stream – The input stream from which to read (e.g., std::cin).
  • str – The std::string object where the input will be stored.
  • delimiter (optional) – The character at which reading stops (defaults to '\n').

Example of getline()

#include<iostream>
#include<string>

int main() {
    std::string name;
    std::cout << "Enter your full name: ";
    
    // Use getline to read the entire line including spaces
    std::getline(std::cin, name);

    std::cout << "Hello, " << name << "!" << std::endl;
    return 0;
}

 

cin.getline() function

The cin.getline() function in C++ is used to read a line of input into a character array (char array). Unlike std::getline(), which is used with std::string, cin.getline() works specifically with C-style strings.

Syntax

std::cin.getline(buffer, size, delimiter);



  • buffer – The character array where the input is stored.
  • size – The maximum number of characters to read (including the null terminator \0).
  • delimiter (optional) – The character at which reading stops (defaults to '\n').

Key Points

  • It stops reading when it encounters the delimiter or when it has read size - 1 characters.
  • It automatically appends a null terminator \0 at the end of the buffer.
  • Unlike std::cin, which stops reading at whitespace, cin.getline() can handle multi-word input with spaces.

 

Example of cin.getline()

#include<iostream>

int main() {
    char name[50]; // Declare a character array to store the input
    std::cout << "Enter your full name: ";
    
    // Use cin.getline to read the input, maximum of 50 characters
    std::cin.getline(name, 50);

    std::cout << "Hello, " << name << "!" << std::endl;
    return 0;
}



Explanation

  • The cin.getline(name, 50); reads a full line of input (up to 49 characters plus the null terminator).
  • If the user inputs "John Doe", the entire string is stored in the name array.

 

Example 2: Using a Custom Delimiter

#include<iostream>

int main() {
    char sentence[100];
    std::cout << "Enter a sentence (end input with a semicolon ';'): ";
    
    // Use cin.getline to read input until a semicolon is encountered
    std::cin.getline(sentence, 100, ';');

    std::cout << "You entered: " << sentence << std::endl;
    return 0;
}



Explanation

  • In this example, the delimiter is set to ';'.
  • If the user types "This is a test; More text", only "This is a test" will be stored in sentence because cin.getline() stops reading at the semicolon.

 

Things to Note

  1. Buffer Overflow: Ensure that the size parameter does not exceed the size of the character array to avoid buffer overflow issues.
  2. End of Input Handling: If the user inputs more characters than the specified size, the extra characters remain in the input buffer and can cause problems for subsequent input operations.
  3. Null Terminator: cin.getline() automatically adds a null terminator \0 at the end of the buffer, so it’s important to allocate enough space in the array.

 

2. write() function

The write() function is part of the output operations and is generally used with std::cout. It outputs a specific number of characters from a char array or std::string.

Syntax

output_stream.write(str, n);

 

Example of write() function

#include<iostream>

int main() {
    const char *text = "Hello, World!";
    
    // Output only the first 5 characters using write
    std::cout.write(text, 5);  // Output: Hello

    std::cout << std::endl;

    // Output the entire text
    std::cout.write(text, 13); // Output: Hello, World!
    
    return 0;
}

Explanation

  • The write() function writes exactly n characters from the provided char array or C-style string.
  • In the example:
    • std::cout.write(text, 5); outputs only "Hello" because it reads only the first 5 characters from text.
    • std::cout.write(text, 13); outputs the entire string "Hello, World!".

 

 


End of the lesson….enjoy learning

 

 

Student Ratings and Reviews

 

5.0
5.0 out of 5 stars (based on 2 reviews)
Excellent100%
Very good0%
Average0%
Poor0%
Terrible0%

 

 

December 3, 2024

Nice !!

December 3, 2024

very beneficial in learning cpp

 

 

Submit a Review

 

 

Layer 1
Login Categories