Szerkesztő:LinguisticMystic/cpp/InputOutput
Every program needs data to work with and usually produces some result. This is where input and output come in. Without I/O, programs would be isolated from the world—unable to get data or share results.
- Input: receiving data from the user, files, devices, or other programs.
- Output: sending data to the screen, files, network connections, or devices.
Modern C++ simplifies I/O using streams, which abstract away the actual device, so your code stays portable and flexible.
🖥️ Console Input and Output
[szerkesztés]
Including the I/O Library
[szerkesztés]To use input and output streams in C++, you include:
#include <iostream>
The std::cout Stream (Console Output)
[szerkesztés]std::cout << "Hello, World!";
std::coutstands for “character output”.- The
<<operator sends data to the output stream.
The std::cin Stream (Console Input)
[szerkesztés]int age;
std::cout << "Enter your age: ";
std::cin >> age;
std::cinstands for “character input”.- The
>>operator extracts input from the stream into a variable.
Note: Input using
cinassumes the correct type. If the user types a string instead of a number, this may lead to errors or unexpected behavior.
🔤 Namespaces and std::
[szerkesztés]C++ uses namespaces to group related code. The C++ Standard Library lives in the std namespace.
Instead of always typing std::cout or std::cin, you can do:
using namespace std;
But in larger projects, explicitly using std:: is safer to avoid name conflicts.
📄 Full Console I/O Example
[szerkesztés]#include <iostream>
using namespace std;
int main() {
int age;
cout << "Enter your age: ";
cin >> age;
cout << "Your age = " << age << endl;
return 0;
}
What Happens Here:
[szerkesztés]- Prompts the user.
- Stores the input in
age. - Prints the result.
📁 File Input and Output in C++
[szerkesztés]To read from or write to files, you use:
#include <fstream>
Writing to a File
[szerkesztés]#include <iostream>
#include <fstream>
using namespace std;
int main() {
ofstream file("text_file.txt"); // output file stream
if (!file) {
cout << "Failed to open file.\n";
return 1;
}
file << "Hello World!!!";
cout << "Text written to file successfully.\n";
return 0;
}
ofstream= output file stream- Acts like
cout, but writes to a file
Reading from a File
[szerkesztés]#include <iostream>
#include <fstream>
using namespace std;
int main() {
ifstream file("text_file.txt"); // input file stream
if (!file) {
cout << "Failed to open file.\n";
return 1;
}
string word;
while (file >> word) {
cout << word << " ";
}
return 0;
}
ifstream= input file stream- Acts like
cin, but reads from a file
⏩ Stream Operators (<< and >>)
[szerkesztés]To remember:
cout <<: data flows to the outputcin >>: data flows from the input
Mnemonic: Imagine the data source is on the left.
<<: send to the right (output)>>: receive from the left (input)
🧾 Summary
[szerkesztés]- Input and output allow programs to communicate with users, files, and other systems.
iostreamhandles console I/O withcinandcout.fstreamhandles file I/O withifstreamandofstream.- Streams simplify I/O by hiding device-specific details.
- Practice with both console and file I/O helps develop solid programming skills.