Skip to content
Question of 22

Q.Find the output of the following C++ code considering that the binary file STOCK.DAT exists on the hard disk with the following 5 records for the class STOCK containing Name and Price. Name Price Rice 110 Wheat 60 Cheese 200 Pulses 170 Sauce 150 void main() { fstream File; File.open("STOCK.DAT",ios::binary|ios::in); Stock S; for (int I=1; I<=2; I++) { File.seekg((2*I-1)sizeof(S)); File.read((char)&S, sizeof(S)); cout << "Read : " << File.tellg()/sizeof(S)<< endl; } File.close(); }

(OR)
Differentiate between seekg() and tellg().
CBSECBSE Class XII Board 2019Subjective· 1mImportance★★★★★
0% · 0/22 Questions
🔒 Locked · start free trial →

You're viewing a preview — the full solution, concept, methods & PYQ mapping are locked.

Start your 14-day free trial to unlock the full solution →

Part (a): the loop repositions and reads, printing Read : 2 then Read : 4. Part (b): seekg() moves the get-pointer to a position, whereas tellg() reports the get-pointer's current position.

Part (a)

Let s = sizeof(S) be the size of one STOCK record. seekg(pos) moves the get-pointer to byte pos, read(...) then reads one record (advancing the pointer by s), and tellg() returns the current byte offset.

  • I = 1: seekg((2·1−1)*s) = seekg(1*s) → pointer at start of record index 1. read consumes one record → pointer now at 2*s. tellg()/sizeof(S) = 2s/s = 2 → prints Read : 2.
  • I = 2: seekg((2·2−1)*s) = seekg(3*s) → pointer at start of record index 3. read → pointer at 4*s. tellg()/sizeof(S) = 4s/s = 4 → prints Read : 4.
Read : 2
Read : 4 …

Unlock everything free for 14 days

  • Full step-by-step solutions
  • Concept-first explanations
  • Methods, shortcuts & mistakes
  • PYQ mapping + timed mock tests

Full access for 14 days. No credit card required.