#includeint main() { std::ofstream outfile ("example.txt"); // Open file for writing if (outfile.is_open()) { outfile << "This is a sample text." << std::endl; // Write text to file outfile.close(); // Close file } }
#includeThis code opens a file named "example.txt" for appending using `std::ofstream`. It then checks if the file is open and appends a new text to the end of the file using the `std::ios_base::app` flag. Finally, it closes the file using the `close()` method. The `std::ofstream` class is part of the C++ standard library which is included in most C++ compilers such as g++, Visual C++, and clang++.int main() { std::ofstream outfile ("example.txt", std::ios_base::app); // Open file for appending if (outfile.is_open()) { outfile << "This text will be appended." << std::endl; // Append text to file outfile.close(); // Close file } }