#include#include int main() { std::ifstream input_file("input.txt"); // Check if file opened successfully if (!input_file.is_open()) { std::cerr << "Error opening file" << std::endl; return 1; } // Read file contents until EOF is reached char c; while (!input_file.eof()) { input_file.get(c); std::cout << c; } input_file.close(); return 0; }
#includeIn this example, we open a file called "input.txt" and check if it was opened successfully. Then we move the file pointer to the end of the file and check if it is at the start, which means the file is empty. Package Library: Standard Library#include int main() { std::ifstream input_file("input.txt"); // Check if file opened successfully if (!input_file.is_open()) { std::cerr << "Error opening file" << std::endl; return 1; } input_file.seekg(0, std::ios_base::end); // Move file pointer to end bool is_empty = input_file.tellg() == 0; // Check if pointer is at start if (is_empty) { std::cout << "File is empty" << std::endl; } else { std::cout << "File is not empty" << std::endl; } input_file.close(); return 0; }