C++读取txt文件数据

本次实验主要目的是实现C++提取txt文件的数据,txt文件中的数据为double型。

txt文件的数据为

1.123456789098 2.123456789098 3.123456789098 
4.123456789098 5.123456789098 6.123456789098
7.123456789097 8.123456789098 9.123456789098 

主要代码为:

  #include <iostream>
  #include <vector>        //提供向量头文件
  #include <algorithm>     // 算法头文件,提供迭代器
  #include <fstream>       //提供文件头文件
  #include <iomanip>       //C++输出精度控制需要
   
  using namespace std;
  int main()
  {
     vector<double> V;
     vector<double>::iterator it;
     ifstream data("t.txt");
     double d;
     while (data >> d)
         V.push_back(d);//将数据压入堆栈。//
     data.close();
     int i = 0;
     for(it = V.begin();it != V.end();it++)
     {
         cout << "V[" << i << "]="<< setprecision(16) << *it << endl;
         i++;
     }
     return 0;
 }

输出为:

猜你喜欢

转载自blog.csdn.net/m0_38036750/article/details/83478602