File Handling
Share:
In C++, the concept of file handling is an essential part. File handling lets a programmer create a new file, write in a file, read from a file, and perform various other operations on files. In simple terms, file handling provides a way to store data in a permanent fashion as opposed to the temporary storage of variables and arrays. This chapter will guide you through the basics of file handling in C++.
To use the file handling capabilities in C++, we'll have to include the fstream header file in our code. This header file contains the declarations of ifstream class for reading from files, ofstream class for writing to files, and fstream class for both reading and writing from/to files.
Here's a simple illustration:
#include <fstream>
The above line needs to be included at the very beginning of your code. If you fail to include the fstream header file, the compiler wouldn't recognize your file input and output operations and will return errors.
Writing to a File
To write to a file in C++, you'll use the ofstream class. Imagine you're a director of a movie called "SpaceWarrior" and you want to write a few lines about it in a file. Here is how you'll do it using ofstream.
#include <fstream>
int main() {
std::ofstream movie_file("SpaceWarrior.txt");
if (movie_file.is_open()) {
movie_file << "SpaceWarrior is a science fiction action film. "
"It is directed by J.J. Abrams and stars Tom Cruise as the lead actor.";
movie_file.close();
} else {
std::cout << "Couldn't open the file.";
}
return 0;
}
In the code above, we first include the fstream header file. Then in the main function, we create an object of the ofstream class called movie_file. We pass the name of the file, "SpaceWarrior.txt", to the constructor of the class. Then we check if the file is open or not using the is_open() function. If the file is open, we write a few lines about the movie into the text file using the insertion operator "<<" and finally close the file using the close() function.
Reading from a File
To read from a file in C++, you'll employ the ifstream class. Let's read the lines we just wrote about the "SpaceWarrior" movie from the SpaceWarrior.txt file.
#include <iostream>
#include <fstream>
#include <string>
int main() {
std::ifstream movie_file("SpaceWarrior.txt");
if (movie_file.is_open()) {
std::string line;
while (getline(movie_file, line)) {
std::cout << line << '\n';
}
movie_file.close();
} else {
std::cout << "Couldn't open the file.";
}
return 0;
}
Here we first include the necessary header files iostream, fstream, and string. Inside the main function, we define an ifstream object called movie_file and pass the name of the file we want to read from its constructor. We then check if the file is open using the is_open() function. If the file is open, we define a string called line and use a while loop along with the getline() function to read each line from the file. Each line is printed to the console as it is read. Finally, we close the file with the close() function.
Open Modes
In C++, you can specify the mode in which you want to open the file. The modes are defined in the fstream header file and are passed as a second argument to the constructor of ifstream, ofstream, and fstream classes.
These modes are:
- std::ios::in – for reading from a file
- std::ios::out – for writing to a file
- std::ios::app – for appending to a file
- std::ios::trunc – for truncating a file
Suppose you want to append to the file named "SpaceWarrior.txt". Here is how you can do it.
#include <fstream>
int main() {
std::ofstream movie_file("SpaceWarrior.txt", std::ios::app);
if (movie_file.is_open()) {
movie_file << "\nSpaceWarrior has been critically acclaimed for its visual effects and storytelling.";
movie_file.close();
} else {
std::cout << "Couldn't open the file.";
}
return 0;
}
Here we used the std::ios::app open mode to append data to the file. If the file "SpaceWarrior.txt" doesn't exist, it will be created, and the line will be written into it.
This gives you a solid start to file handling in C++. There are further functions and methods for file handling, such as checking the size of the file, checking if the end of the file (EOF) has been reached, and others, which you can explore as you dive deeper into C++ programming. Remember, practical implementation is the key to master any programming concept. So make sure you practice these concepts by writing, reading, and manipulating files. Happy Coding!
0 Comment
Sign up or Log in to leave a comment