Files in C++: From Beginner to Pro in 20 Minutes
\n\nImagine your program is an excellent chef. It knows how to cook, but all ingredients must be brought manually, and recipes disappear after each use. Doesn't sound great, right? That's why any self-respecting developer must know how to work with files and the file system. In C++, this isn't just "possible"—it's the foundation, without which you'll feel insecure as a programmer.
\n\nToday, we'll break down working with files in C++ so that you don't just copy the code, but actually understand what's happening under the hood. No boring lectures, just live examples and practical tips. Let's go!
\n\nStreams — the Foundation of File Handling in C++
\n\nIn C++, working with files is built around streams. If you're already familiar with cout and cin, consider half the job done. File streams work on the same principle, only instead of the screen or keyboard, data is sent to a file or loaded from it.
The main players on the field:
\n\n- \n
ifstream— for reading from a file (input file stream). \n ofstream— for writing to a file (output file stream). \n fstream— a universal stream for reading and writing. \n
All these classes live in the header file <fstream>. Don't forget to include it, otherwise the compiler will be upset with you.
How to Open a File Without Losing Your Mind
\n\nOpening a file in C++ is like opening a door: you need to know where to go and why. Let's look at a simple example of opening a file for writing:
\n\n#include <iostream>\n#include <fstream>\n#include <string>\n\nint main() {\n std::ofstream outFile("example.txt"); // Open file for writing\n \n if (outFile.is_open()) {\n outFile << "Hello, file!" << std::endl;\n outFile << "This is the second line." << std::endl;\n outFile.close(); // Close the file\n std::cout << "Write successful!" << std::endl;\n } else {\n std::cerr << "Failed to open file!" << std::endl;\n }\n \n return 0;\n}\n\n\nNotice: we always check if the file opened using is_open(). If the file doesn't exist, ofstream will create it automatically. And if it fails (for example, due to write permissions), the program won't crash but will report an error.
Open Modes: When One Isn't Enough
\n\nBy default, ofstream overwrites the file, and ifstream only reads. But what if you want to append data to the end of a file? Or open a file for both reading and writing simultaneously? That's where mode flags come to the rescue:
- \n
std::ios::in— open for reading. \n std::ios::out— open for writing (default for ofstream). \n std::ios::app— append to the end of the file. \n std::ios::ate— move the cursor to the end upon opening. \n std::ios::binary— open in binary mode. \n
Example of using modes:
\n\nstd::ofstream logFile("log.txt", std::ios::app); // Append to the end\nif (logFile.is_open()) {\n logFile << "New log entry" << std::endl;\n logFile.close();\n}\n\n\nAnd if you need both reading and writing, use fstream with combined flags:
std::fstream file("data.txt", std::ios::in | std::ios::out);\n\n\nReading from a File: By Words and By Lines
\n\nReading from a file in C++ is an art in itself. You can read by words, by lines, by characters. The choice depends on the task.
\n\nReading by Lines (the most popular method)
\n\n#include <fstream>\n#include <iostream>\n#include <string>\n\nint main() {\n std::