Lab P19 β Perform sequential file access.
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
Line3Explanation
- Writes then reads sequentially.
- getline in loop reads all lines.
Viva / Important Questions
Short Questions
- What is sequential access?
- How to read until EOF?
Long Questions
- Explain the program in detail.
- 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.