C++ Files and Streams
Hello, future programmers! Today, we're going to embark on an exciting journey into the world of C++ files and streams. As your friendly neighborhood computer science teacher, I'm here to guide you through this adventure. Trust me, by the end of this lesson, you'll be handling files like a pro!
Opening a File
Let's start with the basics. Imagine you have a treasure chest (that's our file) and you need to open it to either put something in or take something out. In C++, we use something called a "stream" to interact with files.
To open a file, we use the ifstream
class for reading (input) and the ofstream
class for writing (output). If you want to do both, you can use the fstream
class.
Here's how you open a file:
#include <fstream>
#include <iostream>
using namespace std;
int main() {
ofstream myFile("treasure.txt");
if (myFile.is_open()) {
cout << "The treasure chest is open!" << endl;
} else {
cout << "Oh no! We couldn't open the treasure chest." << endl;
}
return 0;
}
In this example, we're trying to open a file named "treasure.txt" for writing. The is_open()
function checks if the file was successfully opened.
Closing a File
After we're done with our treasure chest, it's polite (and good practice) to close it. In C++, files are automatically closed when the file stream object goes out of scope, but it's better to close them explicitly:
myFile.close();
Writing to a File
Now that we have our treasure chest open, let's put some treasure in it! Writing to a file is as easy as using the <<
operator:
ofstream myFile("treasure.txt");
if (myFile.is_open()) {
myFile << "Gold coins: 100" << endl;
myFile << "Magic wands: 3" << endl;
myFile << "Dragon eggs: 1" << endl;
cout << "Treasure added to the chest!" << endl;
} else {
cout << "Failed to open the treasure chest." << endl;
}
myFile.close();
In this example, we're writing three lines to our file. Each <<
operator writes the following text to the file, and endl
adds a new line.
Reading from a File
Reading from a file is like taking inventory of our treasure chest. We use the ifstream
class and the >>
operator to read from a file:
#include <string>
ifstream myFile("treasure.txt");
string line;
if (myFile.is_open()) {
while (getline(myFile, line)) {
cout << line << endl;
}
myFile.close();
} else {
cout << "Unable to open the treasure chest." << endl;
}
Here, we're using getline()
to read each line from the file and print it to the console. The while
loop continues until we reach the end of the file.
Read and Write Example
Let's combine reading and writing in a more complex example. We'll create a program that manages our treasure inventory:
#include <fstream>
#include <iostream>
#include <string>
using namespace std;
int main() {
// Writing to the file
ofstream outFile("inventory.txt");
if (outFile.is_open()) {
outFile << "Gold coins: 100" << endl;
outFile << "Magic wands: 3" << endl;
outFile << "Dragon eggs: 1" << endl;
outFile.close();
cout << "Inventory saved!" << endl;
} else {
cout << "Unable to save inventory." << endl;
return 1;
}
// Reading from the file
ifstream inFile("inventory.txt");
string item;
int count;
if (inFile.is_open()) {
cout << "Current inventory:" << endl;
while (inFile >> item >> count) {
cout << item << " " << count << endl;
}
inFile.close();
} else {
cout << "Unable to read inventory." << endl;
return 1;
}
return 0;
}
In this example, we first write our inventory to a file, then read it back and display it. Notice how we use >>
to read words and numbers separately.
File Position Pointers
Imagine you're reading a book and you want to keep track of where you are. File position pointers do just that for files. They keep track of where we are in the file.
Here are some useful functions for working with file position pointers:
Function | Description |
---|---|
tellg() |
Returns the current position of the get pointer (for input) |
tellp() |
Returns the current position of the put pointer (for output) |
seekg() |
Sets the position of the get pointer |
seekp() |
Sets the position of the put pointer |
Let's see an example:
#include <fstream>
#include <iostream>
using namespace std;
int main() {
fstream file("treasure_map.txt", ios::in | ios::out);
if (!file.is_open()) {
cout << "Failed to open the treasure map!" << endl;
return 1;
}
// Write to the file
file << "X marks the spot!";
// Move to the beginning of the file
file.seekg(0, ios::beg);
// Read the content
string content;
getline(file, content);
cout << "The map says: " << content << endl;
// Move 2 characters from the beginning
file.seekp(2, ios::beg);
// Overwrite part of the content
file << "doesn't always mark";
// Move to the beginning and read again
file.seekg(0, ios::beg);
getline(file, content);
cout << "Now the map says: " << content << endl;
file.close();
return 0;
}
In this example, we're writing to a file, then reading from it, then modifying part of it, and finally reading it again. It's like we're updating our treasure map!
And there you have it, brave explorers of the C++ realm! You've learned how to open treasure chests (files), store your loot (write data), take inventory (read data), and even update your treasure maps (manipulate file contents). Remember, practice makes perfect, so keep coding and exploring. Who knows what digital treasures you'll discover next? Happy coding!
Credits: Image by storyset