optional · CSS study notes
19. Sequential Access Files
Data is read or written in order, from beginning to end — you cannot jump directly to the middle without reading through everything before it.
#include <fstream>
#include <iostream>
using namespace std;
int main()
{
ofstream fout("data.txt");
fout << "Hello CSS aspirant" << endl;
fout.close();
ifstream fin("data.txt");
string line;
getline(fin, line);
cout << "Read back: " << line << endl;
fin.close();
return 0;
}
Best suited for logs, simple text files, or any case where records are always processed start-to-end.
The core constraint: order matters
Sequential access means you always start from the beginning and read forward — there's no way to jump to record 500 without passing through records 1–499 first.
#include <fstream>
#include <iostream>
using namespace std;
int main()
{
// Writing multiple lines sequentially
ofstream fout("students.txt");
fout << "Ali,85" << endl;
fout << "Sara,90" << endl;
fout << "Bilal,78" << endl;
fout.close();
// Reading them back, line by line, IN ORDER
ifstream fin("students.txt");
string line;
while (getline(fin, line)) // reads one line at a time, sequentially
{
cout << line << endl;
}
fin.close();
return 0;
}
Output: all three lines, in the exact order they were written — you cannot ask for just "Bilal's line" without scanning through Ali's and Sara's first.
When sequential access is the right choice
- Log files (always appended, always read start-to-end)
- Simple configuration files
- Any case where you process every record anyway, so jumping around wouldn't help
Contrast, briefly
If a program needs to update just record #47 out of 10,000 without touching the rest, sequential access would force reading through 46 records first — that's when Random Access Files (with seekg/seekp) become necessary instead.
Important MCQs
- In sequential file access, to read record #50 you must:
- a) Jump directly using
seekg()b) Read through records 1–49 first, in order c) Use binary mode only d) Skip to it randomly
- a) Jump directly using
- Sequential access is best suited for:
- a) Frequent random updates b) Logs and files always processed start-to-end c) Databases needing instant lookup d) Only binary files
- Which function is commonly used to read a sequential text file line by line?
- a)
seekg()b)getline()c)write()d)seekp()
- a)
Self-Test Questions
- Why can't you jump directly to the 10th line of a sequential file without reading the first 9?
- Give one real-world example where sequential access is clearly the right choice.
- What C++ function is commonly used to read a sequential text file one line at a time?
📌 Sample & Repeated FPSC Questions (2016–2026)
No FPSC Computer Science question in 2016–2026 isolates "sequential file access" in C++ as its own question. The closest past-paper coverage is the same 2020 Paper-I Q5(b) file-based system on the Files & Streams and Random Access Files pages — its "Find a student by CGPA" operation scans a file sequentially via fin.read() in a loop, though the question itself doesn't name or contrast the "sequential access" concept explicitly.