1. Intro
Unformatted I/O functions operate on raw characters or blocks of data without applying any formatting rules, unlike >> and << which skip whitespace and interpret types. They give finer, low-level control over exactly what is read from or written to a stream, one character (or a specific delimiter) at a time.
Cricket analogy: While a normal scoreboard summary skips extraneous details and reports only clean totals (like >>/<<), a ball-by-ball raw commentary log records every single delivery including wides and dead balls without filtering - that's unformatted I/O.
2. Syntax
char c = cin.get(); // read one character
cin.get(c); // read one character into c
cout.put(c); // write one character
cin.ignore(n, delim); // skip n characters or until delim3. Explanation
cin.get() reads a single character from the input stream, including whitespace characters that cin >> would normally skip, making it useful for character-by-character processing. cout.put() writes a single character to the output stream. cin.ignore(n, delim) discards up to n characters from the input buffer, or stops early if the delimiter character is found — commonly used to clear a leftover newline before a getline() call. These functions are considered unformatted because they do not perform any type conversion or whitespace skipping.
Cricket analogy: cin.get() reading one character including whitespace is like an umpire signaling every single delivery outcome individually, even the dot balls that a summary would normally skip; cin.ignore() is like discarding a rain delay before resuming play.
cin.get() does NOT skip leading whitespace like cin >> does, so calling it right after cin >> var; will often read the leftover newline character instead of the next meaningful character.
4. Example
#include <iostream>
using namespace std;
int main() {
char ch;
cout << "Enter a character: ";
cin.get(ch);
cout << "You entered: ";
cout.put(ch);
cout << endl;
cin.ignore(100, '\n'); // clear rest of the line
return 0;
}5. Output
Enter a character: Z
You entered: Z6. Key Takeaways
cin.get()reads a single character, including whitespace, unlikecin >>.cout.put()writes a single character to the output stream.cin.ignore(n, delim)discards characters, commonly used to clear a leftover newline.- Unformatted I/O functions do not skip whitespace or perform type conversion, unlike
>>and<<.
Practice what you learned
1. What does `cin.get()` read that `cin >>` would normally skip?
2. Which function writes a single character to output?
3. What is `cin.ignore(100, '\n')` commonly used for?
4. Why are these functions called 'unformatted' I/O?
5. What problem can occur if `cin.get()` is called right after `cin >> var;`?
Was this page helpful?
You May Also Like
getline() in C++
Learn how to use `getline()` to read entire lines of text including spaces, and avoid common pitfalls when mixing it with `cin >>`.
cin and cout in C++
Learn how `cin` and `cout` handle standard input and output in C++ using the stream extraction and insertion operators, with chaining and gotchas.
cerr and clog in C++
Understand the difference between `cerr` and `clog` in C++ for writing error and log messages, including buffering behavior.
Related Reading
Related Study Notes in Programming
Browse all study notesApache Spark Study Notes
Programming · 30 topics
ProgrammingApache Flink Study Notes
Programming · 30 topics
ProgrammingHadoop Study Notes
Programming · 30 topics
ProgrammingSnowflake Study Notes
Programming · 30 topics
ProgrammingApache Airflow Study Notes
Programming · 30 topics
Programmingdbt (Data Build Tool) Study Notes
Programming · 30 topics