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?

Difficulty: Easy

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 without .h). Console streams std::cin and std::cout are declared in iostream.h (modern ). Therefore, for file input/output you include fstream.h in the historical naming scheme represented by the options.


Step-by-Step Solution:
Identify the classes to be used: ifstream/ofstream/fstream for files.Recall the corresponding header: fstream.h (or the modern ).Select the option that names the correct file-stream header for the given context.Confirm that iostream.h is for console streams, not file streams specifically.


Verification / Alternative check:
Create a simple program that opens a file with std::ifstream. Including fstream.h (or ) compiles, while including only iostream.h will not declare file stream types.


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 and in contemporary C++ code.


Final Answer:
fstream.h.

More Questions from Object Oriented Programming Using C++

Discussion & Comments

No comments yet. Be the first to comment!
Join Discussion