getline 單向前進,無法「回到上一行」。
#include <vector>
#include <string>
#include <fstream>
using namespace std;
vector lines;
ifstream fin("data.txt");
string line;
while (getline(fin, line)) {
lines.push_back(line);
}
fin.close();
// 隨機存取
string line5 = lines[4]; // 第 5 行 (0-based)
string prev = lines[current - 1]; // 回溯上一行
CStringArray arr;
CStdioFile file;
file.Open(_T("data.txt"), CFile::modeRead);
CString str;
while (file.ReadString(str)) arr.Add(str);
file.Close();
// 存取
CString line = arr.GetAt(nIndex);
mmap/CreateFileMapping) 或**分塊索引**(記錄每行起始偏移量)。