CSV 格式股票數據檔 (日期,開盤,最高,最低,收盤,成交量,成交金額) 需匯入四張表分析。CFile/CStdioFile 內部使用 Far 指標,單次讀寫限制 65535 位元組,超過即觸發 ASSERT 錯誤。
CArchive 配合 CFile 以文字模式讀取,無 64KB 限制:
CFile srcFile; srcFile.Open(...);
CArchive ar(&srcFile, CArchive::load);
CString line;
while (ar.ReadString(line)) {
if (line == "日期,開盤,最高,最低,收盤,成交量,成交金額" || line.IsEmpty())
continue; // 跳過標題列
// 逐欄位解析...
}
int nYear;
CString yearStr = line.Left(line.Find('年')); // 取「年」前字串
nYear = atoi(yearStr);
line = line.Right(line.GetLength() - line.Find('年') - 1); // 移除已解析部分
// 重複上述邏輯依次解析月、日、開盤、最高...
// 使用 Find(',') 定位逗號分隔,Left/Right 擷取子字串,atoi/atof 轉型。
ReadString 自動處理換行,逐行處理邏輯清晰。Find, Left, Right) 解析 CSV 直觀易維護。