In standard C++ file I/O, which header file must be included to use input and/or output file streams such as std::ifstream and std::ofstream?
-
Afilestream.h
-
Bfstream.h
-
Cinstream.h
-
Dinoutstream.h
-
Eiostream.h
Answer
Correct Answer: fstream.h
Explanation
Introduction / Context:C++ uses a standard library of stream classes for console and file I/O. While std::cin and std::cout come from one header, file streams (for reading from and writing to files) come from another. Knowing the correct header improves portability and prevents compilation errors when working with files.
Given Data / Assumptions:
- The question refers to traditional header naming conventions seen in many textbooks (e.g., fstream.h, iostream.h).
- It targets file streams, not console streams.
- We assume basic C++ I/O knowledge.
Concept / Approach:File stream classes std::ifstream, std::ofstream, and std::fstream are declared in the header historically called fstream.h (modern C++ uses
Step-by-Step Solution:Identify the classes to be used: ifstream/ofstream/fstream for files.Recall the corresponding header: fstream.h (or the modern
Verification / Alternative check:Create a simple program that opens a file with std::ifstream. Including fstream.h (or
Why Other Options Are Wrong:
- filestream.h / instream.h / inoutstream.h: Not standard headers.
- iostream.h: Console I/O; does not provide file stream class declarations alone.
Common Pitfalls:Mixing legacy .h headers with modern headers. Prefer
Final Answer:fstream.h.