From 66405b13844446dcd4eb11209946e8441a5f1822 Mon Sep 17 00:00:00 2001 From: pjniyekxf <3358921628@qq.com> Date: Mon, 20 Jan 2025 18:50:43 +0800 Subject: [PATCH] ADD file via upload --- CSVReader.cpp | 58 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 58 insertions(+) create mode 100644 CSVReader.cpp diff --git a/CSVReader.cpp b/CSVReader.cpp new file mode 100644 index 0000000..0a7b607 --- /dev/null +++ b/CSVReader.cpp @@ -0,0 +1,58 @@ +#include "CSVReader.h" + +// 构造函数 +CSVReader::CSVReader(const string& filename) : filename(filename) { + file.open(filename); + if (!file.is_open()) { + cerr << "Error: Could not open file " << filename << endl; + exit(1); + } +} + +// 析构函数 +CSVReader::~CSVReader() { + if (file.is_open()) { + file.close(); + } +} + +// 按行读取CSV文件 +vector CSVReader::readByLine() { + vector rows; + string line; + while (getline(file, line)) { + CSVRow row; + stringstream ss(line); + string cell; + while (getline(ss, cell, ',')) { + row.data.push_back(cell); + } + rows.push_back(row); + } + return rows; +} + +// 按块读取CSV文件 +vector CSVReader::readByBlock(int blockSize) { + vector rows; + string line; + int count = 0; + while (getline(file, line) && count < blockSize) { + CSVRow row; + stringstream ss(line); + string cell; + while (getline(ss, cell, ',')) { + row.data.push_back(cell); + } + rows.push_back(row); + count++; + } + return rows; +} + +// 按字节读取CSV文件 +string CSVReader::readByByte(int byteSize) { + vector buffer(byteSize); + file.read(buffer.data(), byteSize); + return string(buffer.data(), byteSize); +} \ No newline at end of file