Lab P19 β€” Perform sequential file access.

NBC351 Β· Program 19⏱ 8 min read❓ 5 MCQs

Aim

Aim: Demonstrate sequential file access (read/write line by line).

Theory

Demonstrate sequential file access (read/write line by line).

C++ Program

#include 
#include 
using namespace std;
int main(){
    ofstream out("seq.txt");
    out<<"Line1\nLine2\nLine3\n"; out.close();
    ifstream in("seq.txt"); string line;
    while(getline(in,line)) cout<

Expected Output

Line1
Line2
Line3

Explanation

  • Writes then reads sequentially.
  • getline in loop reads all lines.

Viva / Important Questions

Short Questions

  1. What is sequential access?
  2. How to read until EOF?

Long Questions

  1. Explain the program in detail.
  2. Discuss key concepts demonstrated.

MCQ Practice

1. Sequential access reads:

  • In order
  • Randomly
  • Backwards
  • None
Answer: In order.

2. getline returns:

  • stream ref
  • string
  • bool
  • int
Answer: stream ref.

3. while(getline(...)) loops until:

  • EOF
  • Error
  • First line
  • None
Answer: EOF.

4. Sequential is default for:

  • Text files
  • Binary
  • Random
  • None
Answer: Text files.

5. To append use mode:

  • ios::app
  • ios::trunc
  • ios::binary
  • ios::in
Answer: ios::app.

Quick Revision

  • Sequential = in order.
  • Use getline for text.