Input and Output
Input and Output (I/O) in C++ form the backbone of how programs interact with users, files, and other systems. In C++, input allows programs to receive data from external sources, such as keyboards or files, while output enables the program to display information or store results. Mastering I/O is critical for software development and system architecture because efficient data handling is essential for performance, reliability, and maintainability. In modern C++ projects, I/O is not limited to simple console operations; it extends to file streams, string streams, and integration with advanced data structures, algorithms, and object-oriented designs. Through this tutorial, you will explore the full spectrum of C++ I/O capabilities, including standard input/output streams, formatted and unformatted I/O, and error handling during data operations. We will emphasize best practices for safe and efficient I/O, demonstrate how to leverage C++ features such as classes, STL containers, and algorithms, and highlight common pitfalls like memory leaks, inefficient buffering, and poor exception handling. By the end of this tutorial, readers will be able to implement robust I/O routines in their projects, integrate them seamlessly with algorithmic solutions, and understand how proper I/O design contributes to the architecture of high-performance C++ systems.
Basic Example
text\#include <iostream>
\#include <string>
int main() {
std::string name;
int age;
std::cout << "Enter your name: ";
std::getline(std::cin, name);
std::cout << "Enter your age: ";
while (!(std::cin >> age)) {
std::cin.clear(); // Clear error flags
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); // Discard invalid input
std::cout << "Invalid input. Enter a valid age: ";
}
std::cout << "Hello, " << name << "! You are " << age << " years old." << std::endl;
return 0;
}
Practical Example
text\#include <iostream>
\#include <fstream>
\#include <vector>
\#include <algorithm>
\#include <string>
class Student {
public:
std::string name;
int score;
Student(const std::string& n, int s) : name(n), score(s) {}
void display() const {
std::cout << name << ": " << score << std::endl;
}
};
int main() {
std::ifstream inputFile("students.txt");
std::ofstream outputFile("sorted_students.txt");
if (!inputFile) {
std::cerr << "Error opening input file." << std::endl;
return 1;
}
std::vector<Student> students;
std::string name;
int score;
while (inputFile >> name >> score) {
students.emplace_back(name, score);
}
// Sort students by score descending
std::sort(students.begin(), students.end(), [](const Student& a, const Student& b) {
return a.score > b.score;
});
for (const auto& student : students) {
student.display();
outputFile << student.name << " " << student.score << std::endl;
}
inputFile.close();
outputFile.close();
return 0;
}
📊 Reference Table
C++ Element/Concept | Description | Usage Example |
---|---|---|
std::cin | Standard input stream for reading from console | int x; std::cin >> x; |
std::cout | Standard output stream for writing to console | std::cout << "Hello" << std::endl; |
std::getline | Reads entire line including spaces into string | std::string s; std::getline(std::cin, s); |
std::ifstream | Input file stream for reading from files | std::ifstream in("file.txt"); |
std::ofstream | Output file stream for writing to files | std::ofstream out("file.txt"); |
std::vector | Dynamic array container for storing data | std::vector<int> v; v.push_back(10); |
🧠 Test Your Knowledge
Test Your Knowledge
Test your understanding of this topic with practical questions.
📝 Instructions
- Read each question carefully
- Select the best answer for each question
- You can retake the quiz as many times as you want
- Your progress will be shown at the top