C++ 读取txt文件方法读取速度比较

深碍√TFBOYSˉ_ 2023-07-13 13:51 105阅读 0赞

测试程序部分如下:

文本文档中每一行代表一个三维坐标的x,y,z值,中间使用空格隔开

20200312165014529.png

  1. // fread.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
  2. //
  3. // read1 采用fread函数,按字符分配到vector数组
  4. // read2 getline按行读取数据,按字符分配到vector数组
  5. // read3 getline按行读取数据,按字符串分配到vector
  6. // read4 fin按行读取数据,按字符串分配到vector
  7. // read5 采用fread函数,按字符串分配到vector
  8. #include "pch.h"
  9. #include <iostream>
  10. #include<vector>
  11. #include <stdio.h>
  12. #include <stdlib.h>
  13. #include<ctime>
  14. #include<fstream>
  15. #include<string>
  16. #include<sstream>
  17. #include<cassert>
  18. using namespace std;
  19. double str2num(const string str)
  20. {
  21. stringstream ss(str);
  22. double num;
  23. ss >> num;
  24. return num;
  25. }
  26. struct Point
  27. {
  28. double x;
  29. double y;
  30. double z;
  31. };
  32. vector<Point> read1(const char * file)
  33. {
  34. FILE* pFile; //文件指针
  35. long lSize; // 用于文件长度
  36. char* buffer; // 文件缓冲区指针
  37. size_t result; // 返回值是读取的内容数量
  38. vector<string> v;
  39. pFile = fopen(file, "rb");
  40. if (pFile == NULL) { fputs("File error", stderr); exit(1); } // 如果文件错误,退出1
  41. // obtain file size: 获得文件大小
  42. fseek(pFile, 0, SEEK_END); // 指针移到文件末位
  43. lSize = ftell(pFile); // 获得文件长度
  44. rewind(pFile); // 函数rewind()把文件指针移到由stream(流)指定的开始处, 同时清除和流相关的错误和EOF标记
  45. // allocate memory to contain the whole file: 为整个文件分配内存缓冲区
  46. buffer = (char*)malloc(sizeof(char) * lSize); // 分配缓冲区,按前面的 lSize
  47. if (buffer == NULL) { fputs("Memory error", stderr); exit(2); } // 内存分配错误,退出2
  48. // copy the file into the buffer: 该文件复制到缓冲区
  49. result = fread(buffer, 1, lSize, pFile); // 返回值是读取的内容数量
  50. if (result != lSize) { fputs("Reading error", stderr); exit(3); } // 返回值如果不和文件大小,读错误
  51. /* the whole file is now loaded in the memory buffer. */ //现在整个文件载入内存缓冲区
  52. // 读到内存,看自己怎么使用了...............
  53. // ...........
  54. //cout << buffer;
  55. v.push_back(buffer);
  56. cout << "文件大小: " << v[0].size() << endl;
  57. vector<Point>num;
  58. int i = 0;
  59. while (i < v[0].size())
  60. {
  61. Point p;
  62. string num_s = "";
  63. while (v[0][i] != ' ')
  64. {
  65. num_s += v[0][i];
  66. i++;
  67. }
  68. p.x = str2num(num_s);
  69. i++;
  70. num_s = "";
  71. while (v[0][i] != ' ')
  72. {
  73. num_s += v[0][i];
  74. i++;
  75. }
  76. p.y = str2num(num_s);
  77. i++;
  78. num_s = "";
  79. while (v[0][i] != ' ')
  80. {
  81. num_s += v[0][i];
  82. i++;
  83. }
  84. p.z = str2num(num_s);
  85. num.push_back(p);
  86. while ((v[0][i] != '\n') && (i < v[0].size()))
  87. {
  88. i++;
  89. }
  90. i++;
  91. }
  92. // terminate // 文件终止
  93. fclose(pFile);
  94. free(buffer);
  95. }
  96. vector<Point> readTxt2(string file)
  97. {
  98. std::cout << "读取数据中..." << endl;
  99. ifstream infile;
  100. infile.open(file.data()); //将文件流对象与文件连接起来
  101. assert(infile.is_open()); //若失败,则输出错误消息,并终止程序运行
  102. string s;
  103. //char str;
  104. vector<string> ve;
  105. vector<Point> num;
  106. while (getline(infile, s))
  107. {
  108. Point p;
  109. int i = 0;
  110. string num_s = "";
  111. while (s[i] != ' ')
  112. {
  113. num_s += s[i];
  114. i++;
  115. }
  116. p.x = str2num(num_s);
  117. i++;
  118. num_s = "";
  119. while (s[i] != ' ')
  120. {
  121. num_s += s[i];
  122. i++;
  123. }
  124. p.y = str2num(num_s);
  125. i++;
  126. num_s = "";
  127. while (s[i] != ' ')
  128. {
  129. num_s += s[i];
  130. i++;
  131. }
  132. p.z = str2num(num_s);
  133. num.push_back(p);
  134. }
  135. infile.close(); //关闭文件输入流
  136. std::cout << "读取文件完毕..." << endl;
  137. return num;
  138. }
  139. vector<Point> read3(string file)
  140. {
  141. ifstream fin(file.c_str(), ios::in);
  142. char line[500];
  143. string x, y, z;
  144. vector<Point>num;
  145. while (fin.getline(line, sizeof(line)))
  146. {
  147. stringstream words(line); //将读取到的某一行字符串以空格区分开,
  148. // 就是x,y,z坐标写在一行,以空格隔开就可以
  149. words >> x;
  150. words >> y;
  151. words >> z;
  152. Point point;
  153. point.x = atof(x.c_str());
  154. point.y = atof(y.c_str());
  155. point.z = atof(z.c_str());
  156. num.push_back(point);
  157. }
  158. return num;
  159. }
  160. vector<Point> read4(string file)
  161. {
  162. ifstream fin(file);
  163. string x, y, z;
  164. string temp;
  165. vector<Point>num;
  166. while (fin >> temp)
  167. {
  168. stringstream words(temp);
  169. words >> x;
  170. words >> y;
  171. words >> z;
  172. Point point;
  173. point.x = atof(x.c_str());
  174. point.y = atof(y.c_str());
  175. point.z = atof(z.c_str());
  176. num.push_back(point);
  177. }
  178. return num;
  179. }
  180. vector<Point> read5(const char * file)
  181. {
  182. FILE* pFile; //文件指针
  183. long lSize; // 用于文件长度
  184. char* buffer; // 文件缓冲区指针
  185. size_t result; // 返回值是读取的内容数量
  186. vector<string> v;
  187. pFile = fopen(file, "rb");
  188. if (pFile == NULL) { fputs("File error", stderr); exit(1); } // 如果文件错误,退出1
  189. // obtain file size: 获得文件大小
  190. fseek(pFile, 0, SEEK_END); // 指针移到文件末位
  191. lSize = ftell(pFile); // 获得文件长度
  192. rewind(pFile); // 函数rewind()把文件指针移到由stream(流)指定的开始处, 同时清除和流相关的错误和EOF标记
  193. // allocate memory to contain the whole file: 为整个文件分配内存缓冲区
  194. buffer = (char*)malloc(sizeof(char) * lSize); // 分配缓冲区,按前面的 lSize
  195. if (buffer == NULL) { fputs("Memory error", stderr); exit(2); } // 内存分配错误,退出2
  196. // copy the file into the buffer: 该文件复制到缓冲区
  197. result = fread(buffer, 1, lSize, pFile); // 返回值是读取的内容数量
  198. if (result != lSize) { fputs("Reading error", stderr); exit(3); } // 返回值如果不和文件大小,读错误
  199. /* the whole file is now loaded in the memory buffer. */ //现在整个文件载入内存缓冲区
  200. v.push_back(buffer);
  201. vector<Point>num;
  202. string x, y, z;
  203. for (int i = 0; i < v[0].size(); i++)
  204. {
  205. string str = "";
  206. if (v[0][i] != '\n')
  207. {
  208. str += v[0][i];
  209. i++;
  210. }
  211. stringstream words(str);
  212. words >> x;
  213. words >> y;
  214. words >> z;
  215. Point point;
  216. point.x = atof(x.c_str());
  217. point.y = atof(y.c_str());
  218. point.z = atof(z.c_str());
  219. num.push_back(point);
  220. }
  221. // terminate // 文件终止
  222. fclose(pFile);
  223. free(buffer);
  224. return num;
  225. }
  226. int main()
  227. {
  228. float start = clock();
  229. vector<Point> num1 = read1("E:\\data_test\\option-0000.txt");
  230. cout << "read1函数读取用时 : " << clock() - start << endl;
  231. start = clock();
  232. vector<Point> num2 = readTxt2("E:\\data_test\\option-0000.txt");
  233. cout << "read2函数读取用时 : " << clock() - start << endl;
  234. start = clock();
  235. vector<Point> num3 = read3("E:\\data_test\\option-0000.txt");
  236. cout << "read3函数读取用时 : " << clock() - start << endl;
  237. start = clock();
  238. vector<Point> num4 = read4("E:\\data_test\\option-0000.txt");
  239. cout << "read4函数读取用时 : " << clock() - start << endl;
  240. start = clock();
  241. vector<Point> num5 = read5("E:\\data_test\\option-0000.txt");
  242. cout << "read5函数读取用时 : " << clock() - start << endl;
  243. }

原文地址:https://blog.csdn.net/qq_33519317/article/details/98524441

发表评论

表情:
评论列表 (有 0 条评论,105人围观)

还没有评论,来说两句吧...

相关阅读