Using files for input and output
Files are really useful for saving data locally, so we can retrieve it the next time the program is run or analyze the data after the program exits. For all data structures that we create in code and populate with values, the values will get lost after the application quits unless we save them locally or to the server/cloud. Files serve the purpose of containing the saved data. We can create text files, binary files, or even a file with our own encryption. Files are very handy when we want to log errors or generate a crash report.
Getting ready
For this recipe, you will need a Windows machine with a working copy of Visual Studio.
How to do it…
In this recipe, we will find out how to use file handling operations in C++ to write or read from a text file. We can even use C++ operations to create binary files.
Open Visual Studio.
Create a new C++ project.
Select Win32 Console Application.
Add source files called
Source.cpp
,File.h
, andFile.cpp
.Add the following lines...