Input Streams

Use of peek() Method

What are the use cases of peek() in input streams?

Abstract art representing computer programming

The peek() method in input streams allows you to look at the next character in the stream without extracting it. This can be useful in several scenarios.

Checking for End of File

One common use of peek() is to check if you've reached the end of the file (EOF). This is especially useful when you need to verify if more input is available before attempting to read:

#include <iostream>
#include <sstream>

int main() {
  std::istringstream input{"Hello\nWorld\n"};
  std::string extract;

  while (std::getline(input, extract)) {
    std::cout << "Extracted: " << extract << '\n';
    if (input.peek() == EOF) {  
      std::cout << "Reached EOF\n";
    }
  }
}
Extracted: Hello
Extracted: World
Reached EOF

In this example, peek() helps to check if the end of the stream is reached after reading lines.

Lookahead Character

peek() is also useful when you need to decide on an action based on the next character in the stream. For example, you might want to process input differently if the next character is a specific delimiter or a certain value:

#include <iostream>
#include <sstream>

int main() {
  std::istringstream input{"1234A5678"};
  char nextChar;

  while (input.get(nextChar)) {
    if (std::isdigit(input.peek())) {  
      std::cout << "Next is a digit\n";
    } else {
      std::cout << "Next is not a digit\n";
    }
  }
}
Next is a digit
Next is a digit
Next is a digit
Next is not a digit
Next is a digit
Next is a digit
Next is a digit
Next is a digit
Next is not a digit

Here, peek() checks the next character without consuming it, allowing the program to branch logic based on the lookahead character.

Repeated Checks

Using peek(), you can repeatedly check the same character without moving the input position. This is useful in parsers or situations where multiple operations might be needed on the same upcoming character:

#include <iostream>
#include <sstream>

int main() {
  std::istringstream input{"Hello World"};
  char ch;

  while (input.get(ch)) {
    std::cout << "Char: " << ch << '\n';
    std::cout << "Peek: " << static_cast<char>(input.peek()) << '\n'; 
  }
}
Char: H
Peek: e
Char: e
Peek: y
Char: y
Peek:

This program shows how peek() can be used to preview the next character without extracting it.

Using peek() effectively can help manage input streams more precisely, ensuring your program handles input correctly without prematurely consuming characters.

This Question is from the Lesson:

Input Streams

A detailed introduction to C++ Input Streams using std::cin and istringstream. Starting from the basics and progressing up to advanced use cases including creating collections of custom objects from our streams.

Answers to questions are automatically generated and may not have been reviewed.

This Question is from the Lesson:

Input Streams

A detailed introduction to C++ Input Streams using std::cin and istringstream. Starting from the basics and progressing up to advanced use cases including creating collections of custom objects from our streams.

A computer programmer
Part of the course:

Professional C++

Comprehensive course covering advanced concepts, and how to use them on large-scale projects.

Free, unlimited access

This course includes:

  • 125 Lessons
  • 550+ Code Samples
  • 96% Positive Reviews
  • Regularly Updated
  • Help and FAQ
Free, Unlimited Access

Professional C++

Comprehensive course covering advanced concepts, and how to use them on large-scale projects.

Screenshot from Warhammer: Total War
Screenshot from Tomb Raider
Screenshot from Jedi: Fallen Order
Contact|Privacy Policy|Terms of Use
Copyright © 2024 - All Rights Reserved