01-0007 关于C/C++读取文件,并建立指定结构

1. 问题

有一系列的数据文件,以txt的形态存在,具体的格式如下:

123 44 12
124 45 67
23 45 67
...

上述文件中,每行三个int类型的数据,以空格隔开,每行末尾是换行

现在,需要将其读入到内存中,并且存储于向量之中,向量的组织结构如下:

//声明
std::vector<int*> pointVec;

//储存
int* point=new int[3];
point[0]=123;
point[1]=44;
point[2]=12;
pointVec.push_back(point);

个人预测数据量在百万级,因此写文件、读文件成为一个比较重要的过程,毕竟I/O操作是比较消耗时间的。

2. 读取方案

一百万行数据存储在txt文件中大概需要13M,单纯以二进制读取的时间消耗是微秒级别的,一旦中途需要将其拆分为int就会变得很慢。

2.1 混合QT与C++的读取方式


std::vector<int*>* HeatMap::readSegmentInfo(const char* t_file, int* t_header) {
	std::vector<int*>* pointVec = new std::vector<int*>;
	std::ifstream in;
	in.open(t_file, std::ios::in);
	if (!in.is_open()) return nullptr;
	while (std::getline(in, line)) {
		QString tLine = QString::fromLocal8Bit(line.c_str());
		QStringList lineList = tLine.split(" ");
		if (lineList.count() == 3) {
			int* arr = new int[3];
			arr[0] = lineList[0].toInt();
			arr[1] = lineList[1].toInt();
			arr[2] = lineList[2].toInt();
			pointVec->push_back(arr);
		}
	}
	in.close();
}

时间:11s左右

2.2 stringstream的读取方式


std::vector<int*>* HeatMap::readSegmentInfo(const char* t_file, int* t_header) {
	std::vector<int*>* pointVec = new std::vector<int*>;
	std::ifstream in;
	in.open(t_file, std::ios::in);
	if (!in.is_open()) return nullptr;
	std::stringstream buffer;
	buffer << in.rdbuf();
	while (!buffer.eof()) {
		int* arr = new int[3];
		buffer >> arr[0];
		buffer >> arr[1];
		buffer >> arr[2];
		pointVec->push_back(arr);
	}
	in.close();
}

时间:11s左右

2.3 C风格fscanf()的读取方式


std::vector<int*>* HeatMap::readSegmentInfo(const char* t_file, int* t_header) {
	std::vector<int*>* pointVec = new std::vector<int*>;
	FILE* fp;
	char line[1000] = { 0 };
	fp = fopen(t_file, "r");
	while (!feof(fp)) {
		int* arr = new int[3];
		fscanf(fp, "%d", &arr[0]);
		fscanf(fp, "%d", &arr[1]);
		fscanf(fp, "%d", &arr[2]);
		pointVec->push_back(arr);
	}
	fclose(fp);
}
//也可以手动逐个字符的读取,通过空格换行作为中断或者结束标志,
//手动将buffer转化为int最后存储在向量中,这样也能够达到效果,
//时间与直接使用fscanf相差不大。

时间:2~3s [即使已经很快了,但是依旧觉得慢]

3. 思考&相关链接

不知道是多久以前写的了,停止思考😭

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值