C++ 读取txt文件方法读取速度比较
测试程序部分如下:
文本文档中每一行代表一个三维坐标的x,y,z值,中间使用空格隔开
// fread.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//
// read1 采用fread函数,按字符分配到vector数组
// read2 getline按行读取数据,按字符分配到vector数组
// read3 getline按行读取数据,按字符串分配到vector
// read4 fin按行读取数据,按字符串分配到vector
// read5 采用fread函数,按字符串分配到vector
#include "pch.h"
#include <iostream>
#include<vector>
#include <stdio.h>
#include <stdlib.h>
#include<ctime>
#include<fstream>
#include<string>
#include<sstream>
#include<cassert>
using namespace std;
double str2num(const string str)
{
stringstream ss(str);
double num;
ss >> num;
return num;
}
struct Point
{
double x;
double y;
double z;
};
vector<Point> read1(const char * file)
{
FILE* pFile; //文件指针
long lSize; // 用于文件长度
char* buffer; // 文件缓冲区指针
size_t result; // 返回值是读取的内容数量
vector<string> v;
pFile = fopen(file, "rb");
if (pFile == NULL) { fputs("File error", stderr); exit(1); } // 如果文件错误,退出1
// obtain file size: 获得文件大小
fseek(pFile, 0, SEEK_END); // 指针移到文件末位
lSize = ftell(pFile); // 获得文件长度
rewind(pFile); // 函数rewind()把文件指针移到由stream(流)指定的开始处, 同时清除和流相关的错误和EOF标记
// allocate memory to contain the whole file: 为整个文件分配内存缓冲区
buffer = (char*)malloc(sizeof(char) * lSize); // 分配缓冲区,按前面的 lSize
if (buffer == NULL) { fputs("Memory error", stderr); exit(2); } // 内存分配错误,退出2
// copy the file into the buffer: 该文件复制到缓冲区
result = fread(buffer, 1, lSize, pFile); // 返回值是读取的内容数量
if (result != lSize) { fputs("Reading error", stderr); exit(3); } // 返回值如果不和文件大小,读错误
/* the whole file is now loaded in the memory buffer. */ //现在整个文件载入内存缓冲区
// 读到内存,看自己怎么使用了...............
// ...........
//cout << buffer;
v.push_back(buffer);
cout << "文件大小: " << v[0].size() << endl;
vector<Point>num;
int i = 0;
while (i < v[0].size())
{
Point p;
string num_s = "";
while (v[0][i] != ' ')
{
num_s += v[0][i];
i++;
}
p.x = str2num(num_s);
i++;
num_s = "";
while (v[0][i] != ' ')
{
num_s += v[0][i];
i++;
}
p.y = str2num(num_s);
i++;
num_s = "";
while (v[0][i] != ' ')
{
num_s += v[0][i];
i++;
}
p.z = str2num(num_s);
num.push_back(p);
while ((v[0][i] != '\n') && (i < v[0].size()))
{
i++;
}
i++;
}
// terminate // 文件终止
fclose(pFile);
free(buffer);
}
vector<Point> readTxt2(string file)
{
std::cout << "读取数据中..." << endl;
ifstream infile;
infile.open(file.data()); //将文件流对象与文件连接起来
assert(infile.is_open()); //若失败,则输出错误消息,并终止程序运行
string s;
//char str;
vector<string> ve;
vector<Point> num;
while (getline(infile, s))
{
Point p;
int i = 0;
string num_s = "";
while (s[i] != ' ')
{
num_s += s[i];
i++;
}
p.x = str2num(num_s);
i++;
num_s = "";
while (s[i] != ' ')
{
num_s += s[i];
i++;
}
p.y = str2num(num_s);
i++;
num_s = "";
while (s[i] != ' ')
{
num_s += s[i];
i++;
}
p.z = str2num(num_s);
num.push_back(p);
}
infile.close(); //关闭文件输入流
std::cout << "读取文件完毕..." << endl;
return num;
}
vector<Point> read3(string file)
{
ifstream fin(file.c_str(), ios::in);
char line[500];
string x, y, z;
vector<Point>num;
while (fin.getline(line, sizeof(line)))
{
stringstream words(line); //将读取到的某一行字符串以空格区分开,
// 就是x,y,z坐标写在一行,以空格隔开就可以
words >> x;
words >> y;
words >> z;
Point point;
point.x = atof(x.c_str());
point.y = atof(y.c_str());
point.z = atof(z.c_str());
num.push_back(point);
}
return num;
}
vector<Point> read4(string file)
{
ifstream fin(file);
string x, y, z;
string temp;
vector<Point>num;
while (fin >> temp)
{
stringstream words(temp);
words >> x;
words >> y;
words >> z;
Point point;
point.x = atof(x.c_str());
point.y = atof(y.c_str());
point.z = atof(z.c_str());
num.push_back(point);
}
return num;
}
vector<Point> read5(const char * file)
{
FILE* pFile; //文件指针
long lSize; // 用于文件长度
char* buffer; // 文件缓冲区指针
size_t result; // 返回值是读取的内容数量
vector<string> v;
pFile = fopen(file, "rb");
if (pFile == NULL) { fputs("File error", stderr); exit(1); } // 如果文件错误,退出1
// obtain file size: 获得文件大小
fseek(pFile, 0, SEEK_END); // 指针移到文件末位
lSize = ftell(pFile); // 获得文件长度
rewind(pFile); // 函数rewind()把文件指针移到由stream(流)指定的开始处, 同时清除和流相关的错误和EOF标记
// allocate memory to contain the whole file: 为整个文件分配内存缓冲区
buffer = (char*)malloc(sizeof(char) * lSize); // 分配缓冲区,按前面的 lSize
if (buffer == NULL) { fputs("Memory error", stderr); exit(2); } // 内存分配错误,退出2
// copy the file into the buffer: 该文件复制到缓冲区
result = fread(buffer, 1, lSize, pFile); // 返回值是读取的内容数量
if (result != lSize) { fputs("Reading error", stderr); exit(3); } // 返回值如果不和文件大小,读错误
/* the whole file is now loaded in the memory buffer. */ //现在整个文件载入内存缓冲区
v.push_back(buffer);
vector<Point>num;
string x, y, z;
for (int i = 0; i < v[0].size(); i++)
{
string str = "";
if (v[0][i] != '\n')
{
str += v[0][i];
i++;
}
stringstream words(str);
words >> x;
words >> y;
words >> z;
Point point;
point.x = atof(x.c_str());
point.y = atof(y.c_str());
point.z = atof(z.c_str());
num.push_back(point);
}
// terminate // 文件终止
fclose(pFile);
free(buffer);
return num;
}
int main()
{
float start = clock();
vector<Point> num1 = read1("E:\\data_test\\option-0000.txt");
cout << "read1函数读取用时 : " << clock() - start << endl;
start = clock();
vector<Point> num2 = readTxt2("E:\\data_test\\option-0000.txt");
cout << "read2函数读取用时 : " << clock() - start << endl;
start = clock();
vector<Point> num3 = read3("E:\\data_test\\option-0000.txt");
cout << "read3函数读取用时 : " << clock() - start << endl;
start = clock();
vector<Point> num4 = read4("E:\\data_test\\option-0000.txt");
cout << "read4函数读取用时 : " << clock() - start << endl;
start = clock();
vector<Point> num5 = read5("E:\\data_test\\option-0000.txt");
cout << "read5函数读取用时 : " << clock() - start << endl;
}
原文地址:https://blog.csdn.net/qq_33519317/article/details/98524441
还没有评论,来说两句吧...