Understanding fileno: In the program that opens "DUMMY.C" for writing and assigns t = fileno(fp);, what does t represent?

Difficulty: Easy

Correct Answer: The handle associated with "DUMMY.C" file

Explanation:


Introduction / Context:
Standard I/O in C provides buffered FILE* streams, while lower-level system calls use integer file descriptors (often called handles on some platforms). The fileno function bridges these layers by retrieving the underlying descriptor from a FILE* stream, which can be useful for system-level operations.


Given Data / Assumptions:

  • The code opens a file with fopen and obtains a FILE* named fp.
  • t = fileno(fp); is evaluated.
  • We assume a successful fopen and a standard C library where fileno is available.


Concept / Approach:

  • fileno(FILE stream) returns the integer file descriptor associated with the given stream.
  • This descriptor is used by low-level I/O functions like read, write, or by OS-specific APIs.
  • It is not the size of the file and is not an undefined garbage value when fp is valid.


Step-by-Step Solution:

Call fopen → receive a valid FILE (fp).Call fileno(fp) → get integer descriptor tied to fp.Assign to t → t now holds the OS file handle/file descriptor number.


Verification / Alternative check:

Printing t typically shows small integers (e.g., 3, 4, 5) corresponding to descriptors opened by the process.


Why Other Options Are Wrong:

  • File size: fileno does not compute sizes; use fseek/ftell or stat for that.
  • Garbage value: Only if fp were invalid; with a valid stream, the descriptor is meaningful.
  • Error in fileno(): Not when called correctly with a valid stream.


Common Pitfalls:

  • Using fileno portably across platforms requires care; names differ on some systems.
  • Mixing buffered stdio with unbuffered system calls can require explicit fflush/fsync.


Final Answer:

The handle associated with "DUMMY.C" file

More Questions from Input / Output

Discussion & Comments

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