用C++从txt文件中读取 x 行 y 列的数据到数组中。

C++读取txt文档到数组
//读取数据到 double数组
#include <iostream>
#include <fstream>
 
using namespace std;
 
int main()
{
    double array[27][30]={0.0};//如果数据量过大 则需要把 array 定义成static类型,
                                //因为默认的堆栈大小容量不够,可以放到静态存储区
     
    ifstream infile;//定义文件流对象
     
    infile.open("data.txt");//打开文档
     
    double* ptr = &array[0][0];//定义
     
    while(!infile.eof())
    {
        infile>>*ptr;//这个是把文档里面的数对应在ptr位置的数值上
        ptr++;
    }
     
    infile.close();
     
    return 0;
}

//读取数据到结构体数组
#include <iostream>
#include <fstream>
#include <vector>
 
using namespace std;
 
int main()
{
    vector<double> v;
     
    ifstream infile;
     
    infile.open("data.txt");
     
    double tmp;
    while(!infile.eof())
    {
        infile>>tmp;
        v.push_back(tmp);
    }
     
    infile.close();
     
    return 0;
}

相关文章:

  • 2021-12-18
  • 2022-03-06
  • 2021-08-30
  • 2022-12-23
  • 2022-12-23
  • 2021-11-01
  • 2022-02-14
  • 2021-07-16
猜你喜欢
  • 2022-12-23
  • 2021-09-16
  • 2022-12-23
  • 2021-05-29
  • 2022-01-28
  • 2021-11-18
相关资源
相似解决方案