C++ / C++ File I/O and Streams
Introduction to File Handling in C++
This tutorial will introduce you to the basics of file handling in C++. You'll learn how to open, read, write, and close files using the C++ standard library.
Section overview
5 resourcesCovers file handling and working with streams in C++.
1. Introduction
This tutorial aims to introduce you to the basics of file handling in C++. File handling is an essential part of programming that deals with creating, reading, writing, and closing files.
By the end of this tutorial, you will learn how to:
- Open a file
- Read from a file
- Write to a file
- Close a file
Prerequisites: A basic understanding of C++ programming language.
2. Step-by-Step Guide
Opening a File
In C++, we use the fstream library to work with files. The fstream library provides the fstream class to read and write to files.
To open a file, we use open function of fstream or ofstream or ifstream objects.
fstream myFile;
myFile.open("example.txt");
Reading from a File
To read from a file, we use the >> operator.
string lineData;
myFile >> lineData;
Writing to a File
To write to a file, we use the << operator.
myFile << "This is a new line of text";
Closing a File
After we are done with our operations on the file, we close the file by calling the close method of the fstream object.
myFile.close();
3. Code Examples
Example 1: Writing to a file
#include <fstream>
int main() {
std::ofstream myFile;
myFile.open("example.txt");
if (myFile.is_open()) {
myFile << "This is a new line of text\n";
}
myFile.close();
return 0;
}
This C++ code opens a file named "example.txt", writes a line of text to it, and then closes the file.
Example 2: Reading from a file
#include <fstream>
#include <string>
#include <iostream>
int main() {
std::ifstream myFile;
myFile.open("example.txt");
if (myFile.is_open()) {
std::string lineData;
while (getline(myFile, lineData)) {
std::cout << lineData << "\n";
}
}
myFile.close();
return 0;
}
This C++ code opens a file named "example.txt", reads line by line from it, prints each line to the console, and then closes the file.
4. Summary
In this tutorial, you learned the basics of file handling in C++. You learned how to open, read from, write to, and close files using the fstream library in C++.
For further learning, you can explore how to handle errors during file operations and how to work with binary files.
5. Practice Exercises
- Write a C++ program to write the numbers from 1 to 10 to a file.
- Write a C++ program to read the file created in the previous exercise and print the numbers to the console.
- Write a C++ program that asks the user for a number n and a string, then writes the string to a file n times.
Here are the solutions for the above exercises:
- Here is how you can write numbers from 1 to 10 to a file:
#include <fstream>
int main() {
std::ofstream myFile;
myFile.open("numbers.txt");
if (myFile.is_open()) {
for (int i = 1; i <= 10; i++) {
myFile << i << "\n";
}
}
myFile.close();
return 0;
}
- Here is how you can read the numbers from the file:
#include <fstream>
#include <string>
#include <iostream>
int main() {
std::ifstream myFile;
myFile.open("numbers.txt");
if (myFile.is_open()) {
std::string lineData;
while (getline(myFile, lineData)) {
std::cout << lineData << "\n";
}
}
myFile.close();
return 0;
}
- Here is how you can write a string to a file n times:
#include <fstream>
#include <string>
#include <iostream>
int main() {
std::ofstream myFile;
myFile.open("repeatedString.txt");
if (myFile.is_open()) {
std::string myString;
int n;
std::cout << "Enter a string: ";
getline(std::cin, myString);
std::cout << "Enter a number: ";
std::cin >> n;
for (int i = 0; i < n; i++) {
myFile << myString << "\n";
}
}
myFile.close();
return 0;
}
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.
Latest 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