Welcome to our deep dive into C++ File Pointers! In this comprehensive guide, we'll explore how to work with files using C++, a powerful programming language. By the end of this lesson, you'll be able to create, read, write, and manipulate files in your C++ projects. šÆ
In C++, a file pointer is a variable that keeps track of the current position in a file. It allows us to read and write data from and to specific locations within a file.
File pointers are essential when working with files in C++. They enable us to:
Before we dive into file pointers, let's take a quick look at file streams in C++.
A file stream is an abstract concept representing an input or output device, such as a file. In C++, we use streams to work with files.
#include <fstream>
using namespace std;Here, we include the fstream library, which provides classes for file streams. We also use the namespace std to simplify our code.
To create a file pointer, we use the fstream class and its constructors. Here's an example of creating a file pointer for both reading and writing:
fstream file("example.txt", ios::in | ios::out | ios::trunc);In this example, we create a file pointer named file that opens the file example.txt for both input and output operations (ios::in and ios::out). Additionally, we use ios::trunc to overwrite the existing file if it already exists, or create a new file if it doesn't.
Now that we have a file pointer, let's look at some basic operations:
To read data from a file, we use the getline() function. Here's an example:
string line;
while (getline(file, line)) {
cout << line << endl;
}In this example, we read lines from the file and print them to the console.
To write data to a file, we can use the << operator:
file << "Hello, World!\n";In this example, we write "Hello, World!\n" to the file.
Let's take a look at a more advanced example that demonstrates working with file pointers. We'll create a simple address book application that allows us to add, read, and delete addresses.
#include <iostream>
#include <fstream>
#include <vector>
#include <string>
struct Address {
std::string name;
std::string address;
std::string phone;
};
std::vector<Address> addresses;
void loadAddresses() {
std::ifstream file("addresses.txt", std::ios::in);
Address temp;
while (file >> temp.name >> temp.address >> temp.phone) {
addresses.push_back(temp);
}
file.close();
}
void saveAddresses() {
std::ofstream file("addresses.txt", std::ios::out | std::ios::trunc);
for (const auto& address : addresses) {
file << address.name << " " << address.address << " " << address.phone << "\n";
}
file.close();
}
void addAddress(const Address& address) {
addresses.push_back(address);
saveAddresses();
}
void deleteAddress(int index) {
if (index >= 0 && index < addresses.size()) {
addresses.erase(addresses.begin() + index);
saveAddresses();
} else {
std::cout << "Invalid index.\n";
}
}
void displayAddresses() {
for (const auto& address : addresses) {
std::cout << "Name: " << address.name << "\n";
std::cout << "Address: " << address.address << "\n";
std::cout << "Phone: " << address.phone << "\n";
std::cout << "\n";
}
}
int main() {
loadAddresses();
displayAddresses();
Address newAddress{"John Doe", "123 Main St", "555-1234"};
addAddress(newAddress);
displayAddresses();
deleteAddress(1);
displayAddresses();
return 0;
}In this example, we create a Address struct to hold our address data. We also have functions to load, save, add, and delete addresses from our file. In the main function, we demonstrate using these functions to create, read, and manipulate our address book.
What are file pointers in C++?
With this comprehensive lesson on C++ file pointers, you should now feel confident in your ability to work with files using C++. Happy coding! š