Is cout faster than printf?
iostream can be slower due to formatting and synchronization; printf is C-style. For learning, cout is clearer and type-safe.
Master C++ Input/Output operations with short focused examples. Learn console I/O with cin and cout, file operations, stream formatting, and best practices for efficient I/O handling.
Reading user input
Displaying results
Reading/writing files
Custom output formats
Programs communicate through streams. The iostream library provides type-safe input and output—essential for debugging, CLIs, and logging before you tackle files and networking.
I/O appears in every assignment and interview coding round. Clean output formatting also improves user-facing tools you build.
C++ uses streams for Input/Output operations. Streams are sequences of bytes that flow between your program and input/output devices (keyboard, screen, files). The iostream library provides the foundation for all I/O operations in C++.
Object of istream class for reading input from keyboard. Uses extraction operator >>.
Object of ostream class for writing output to screen. Uses insertion operator <<.
Unbuffered stream for error messages. Output appears immediately without buffering.
Buffered version of cerr for logging. More efficient for frequent error messages.
>> for input (extraction), << for output (insertion)#include <iostream> required for I/O operationsint age;
cout << "Age: ";
cin >> age;double salary;
cin >> salary;string name;
cin >> name;int a, b;
cin >> a >> b;char grade;
cin >> grade;cout << "Enter n: ";
int n; cin >> n;cout << "Hello, World!" << endl;int x = 10;
cout << "x = " << x;cout << "Sum: " << 5 + 3 << endl;cout << "Line1\nLine2";cout << "A\tB\tC";cerr << "Error: invalid input";cin skips leading whitespace (spaces, tabs, newlines)cin >> stops reading at whitespace (can't read strings with spaces)endl adds newline and flushes the output buffer\n adds newline without flushing (more efficient)string name;
getline(cin, name);int age; cin >> age;
cin.ignore();
getline(cin, name);string full = "John Doe";
getline(cin, full);string city, state;
getline(cin, city);
getline(cin, state);string line;
getline(cin, line);
cout << line;string s;
getline(cin, s);
if (s.empty()) cout << "Empty";int n;
if (cin >> n)
cout << "OK: " << n;cin.clear(); // reset error flagcin.ignore(1000, '\n');int n; cin >> n;
if (n >= 1 && n <= 100)
cout << "Valid";char ch; cin >> ch;
ch = tolower(ch);while (!(cin >> n)) {
cin.clear();
cin.ignore(1000, '\n');
}cin >> and getline() without clearing buffercin.clear()cout << fixed << setprecision(2);
cout << 99.9876; // 99.99cout << scientific << 1234.5;int n = 255;
cout << hex << n; // ffcout << setw(10) << 42;cout << left << setw(8) << "Name";bool f = true;
cout << boolalpha << f; // trueendlcout << "Hello" << endl;setw(n)cout << setw(10) << num;setprecision(n)cout << setprecision(2) << price;fixedcout << fixed << num;scientificcout << scientific << num;left / rightcout << left << setw(10) << text;setfill(c)cout << setfill('*') << setw(10) << num;boolalpha / noboolalphacout << boolalpha << flag;ofstream out("data.txt");
out << "Hello" << endl;
out.close();ifstream in("data.txt");
string line;
while (getline(in, line))
cout << line;ifstream f("x.txt");
if (!f) cerr << "Open failed";ofstream a("log.txt", ios::app);
a << "New line\n";ofstream f("nums.txt");
f << 42 << " " << 3.14;fstream fs("data.txt",
ios::in | ios::out);| File Mode | Description | Example |
|---|---|---|
ios::in |
Open for reading (default for ifstream) | ifstream file("data.txt", ios::in); |
ios::out |
Open for writing (default for ofstream) | ofstream file("data.txt", ios::out); |
ios::app |
Append to end of file | ofstream file("data.txt", ios::app); |
ios::ate |
Open and seek to end of file | fstream file("data.txt", ios::ate); |
ios::trunc |
Truncate file if it exists | ofstream file("data.txt", ios::trunc); |
ios::binary |
Open in binary mode | ifstream file("data.bin", ios::binary); |
string name;
getline(cin, name);double total = 0;
for (int i = 0; i < n; i++) {
double m; cin >> m;
total += m;
}double avg = total / n;
cout << avg;char g;
if (avg >= 90) g = 'A';
else if (avg >= 80) g = 'B';cout << left << setw(12)
<< "Average:" << avg;ofstream f("report.txt", ios::app);
f << name << " " << avg << endl;getline() for strings with spacescin >> before getline()\n instead of endl unless flushing is needed#include <iostream> - Standard I/O#include <iomanip> - Formatting#include <fstream> - File I/O#include <string> - String handling
\n instead of endl when possiblecout calls in loopsMastering C++ I/O is essential for creating interactive programs. Start with simple cin/cout operations, then progress to file I/O and advanced formatting. Always handle errors gracefully and validate user input. Remember that efficient I/O can significantly impact your program's performance, especially in console applications.
iostream can be slower due to formatting and synchronization; printf is C-style. For learning, cout is clearer and type-safe.
Use std::endl or '\n'. endl also flushes the buffer, which can affect performance in tight loops.
Often mixing cin >> with getline, or leftover newline in the buffer. Clear cin or use getline consistently.