Let's test your knowledge. Fill in the missing part by typing it in.
To update data in a file, you can use the ofstream
class along with the ios::app
flag similar to appending. However, instead of appending, you can modify specific lines or sections within the file.
Here's an example code that demonstrates how to update data in a file using ofstream
:
TEXT/X-C++SRC
1#include <iostream>
2#include <fstream>
3
4int main() {
5 std::ofstream file("example.txt", std::ios::app);
6
7 if (file.is_open()) {
8 file << "This is an updated line";
9 file.close();
10 std::cout << "Data updated in file." << std::endl;
11 } else {
12 std::cout << "Unable to open file" << std::endl;
13 }
14
15 return 0;
16}
Write the missing line below.