C++ / C++ File I/O and Streams
Reading and Writing Files with Streams
In this tutorial, you'll learn how to use streams in C++ to read from and write to files. We'll cover the basic operations as well as some advanced techniques.
Section overview
5 resourcesCovers file handling and working with streams in C++.
Introduction
In this tutorial, you will learn how to use streams in C++ to read and write files. Specifically, we will cover the use of ifstream (input file stream) and ofstream (output file stream) classes provided by the C++ Standard Library.
By the end of this tutorial, you will be able to:
- Understand the concept of streams in C++
- Read data from a file using ifstream
- Write data to a file using ofstream
- Implement advanced file handling techniques
Prerequisites: Basic knowledge of C++ programming is required to fully understand the concepts explained in this tutorial.
Step-by-Step Guide
In C++, a stream is a sequence of bytes where we can write or read data. A file stream is just a stream associated with a file.
Reading from a File
Use the ifstream class to read data from a file. Here's how to do it:
- Create an object of the ifstream class.
- Use the open() function to open a file.
- Use the >> operator to read data from the file.
- Close the file using the close() function.
Writing to a File
Use the ofstream class to write data to a file. Here's how to do it:
- Create an object of the ofstream class.
- Use the open() function to open a file.
- Use the << operator to write data to the file.
- Close the file using the close() function.
Code Examples
Let's see some practical examples.
Reading from a File
#include <iostream>
#include <fstream>
using namespace std;
int main () {
ifstream myfile; // Create an object of ifstream
myfile.open ("example.txt"); // Open a file
if (myfile.is_open()) { // If the file is open, read from it
string line;
while (getline (myfile, line)) {
cout << line << '\n'; // Output the text from the file
}
myfile.close(); // Close the file
}
else cout << "Unable to open file";
return 0;
}
In the above example, we're reading line by line from the file "example.txt" and printing each line.
Writing to a File
#include <iostream>
#include <fstream>
using namespace std;
int main () {
ofstream myfile; // Create an object of ofstream
myfile.open ("example.txt"); // Open a file
if (myfile.is_open()) { // If the file is open, write to it
myfile << "Hello World!\n"; // Write to the file
myfile.close(); // Close the file
}
else cout << "Unable to open file";
return 0;
}
In this example, we're writing the string "Hello World!" to the file "example.txt".
Summary
In this tutorial, you've learned how to use ifstream and ofstream in C++ to read from and write to files. You can now perform basic file operations using streams in C++.
Next, you might want to learn more about handling errors during file operations, or explore more advanced file handling techniques.
Practice Exercises
- Write a program that reads a text file and counts the number of lines in it.
- Write a program that copies the contents of one file to another.
- Write a program that writes the numbers 1 to 10 on separate lines in a text file.
Solutions and explanations will be provided in the next tutorial. To practice further, you may want to try modifying these programs or creating your own.
Need Help Implementing This?
We build custom systems, plugins, and scalable infrastructure.
Related topics
Keep learning with adjacent tracks.
Popular tools
Helpful utilities for quick tasks.
Random Password Generator
Create secure, complex passwords with custom length and character options.
Use toolLatest articles
Fresh insights from the CodiWiki team.
AI in Drug Discovery: Accelerating Medical Breakthroughs
In the rapidly evolving landscape of healthcare and pharmaceuticals, Artificial Intelligence (AI) in drug dis…
Read articleAI in Retail: Personalized Shopping and Inventory Management
In the rapidly evolving retail landscape, the integration of Artificial Intelligence (AI) is revolutionizing …
Read articleAI in Public Safety: Predictive Policing and Crime Prevention
In the realm of public safety, the integration of Artificial Intelligence (AI) stands as a beacon of innovati…
Read articleAI in Mental Health: Assisting with Therapy and Diagnostics
In the realm of mental health, the integration of Artificial Intelligence (AI) stands as a beacon of hope and…
Read articleAI in Legal Compliance: Ensuring Regulatory Adherence
In an era where technology continually reshapes the boundaries of industries, Artificial Intelligence (AI) in…
Read article