#include#include int main() { std::ofstream outfile("data.txt"); // Check if the file stream is ready for output if (outfile.good()) { outfile << "Hello World!"; // Write data to the file outfile.close(); // Close the file stream } else { std::cout << "Error writing data to file." << std::endl; } return 0; }
#includeIn this example, we create a file stream using the ifstream class to read data from the "data.txt" file. We check if the stream is ready for input and if the file exists using the good() function and the ifstream object itself. If the file exists, we output a message to the console. Otherwise, we output a different message to the console. Package Library: Standard Library#include int main() { std::ifstream infile("data.txt"); // Check if the file stream is ready for input and exists if (infile.good() && infile) { std::cout << "File exists." << std::endl; } else { std::cout << "File does not exist." << std::endl; } return 0; }