File Handling
File Handling allows you to work with files. Using file handling, you can read from files and write into files. To work with files, you need to include fstream header file.
Note: When you use file handling in code, a file is created and any input user enters saves in the file.
Writing to a file
To write data to a file, you can use ofstream class from fstream header file. Create an object and open a file using .open() function.
Reading from a file
To read data from a file, you can use ifstream class from fstream header file. Create an object and open a file using .open() function.
Appending to a file
To append data to an existing file, you can use ofstream class with append function. Create an object and open a file using .open() function.
In the following code, we are creating a file, and when we execute the program a file is created in the directory where your program is located. When you give input on console, the input will be saved in that file.
Practice Exercises
Complete these exercises to reinforce your learning and earn XP