1.读取长度不一用分隔符隔开的点名以及X,Y,Z到vector向量中
#include<vector>
#include<sstream>
#include<iostream>
//函数参数加上&可以变成实参
void read_point(string& filename, vector<string>& P_name, vector<double>& X, vector<double>& Y, vector<double>& Z, char separator = ',')
std::ifstream file(filename.c_str(), ifstream::in);
if (!file)
{
cerr << "open error!";
}
string line, p, x, y, z;
while (getline(file, line))
{
stringstream liness(line);
getline(liness, p, separator);
getline(liness, x, separator);
getline(liness, y, separator);
getline(liness, z);
if (!p.empty() && !x.empty() && !y.empty() && !z.empty())
{
P_name.push_back(p);
X.push_back(stod(x));
Y.push_back(stod(y));
Z.push_back(stod(z));
}
}
}
2.读取固定长度的数据
#include<iostream>
#include<string>
//把每行数据读取到s[n]数组
ifstream infile("file");
if (!infile)
{
cerr << "open error!" << endl;
abort();
}
string s[n];
for (int j = 0; j < n; j++)
{
getline(infile, s[j]);
}
infile.close();
//定义数据
int x = stoi(s[a].substr(b, c));//第a行数据,从第b个字节开始读取c个字节到整型x里
double y = stod(s[a].substr(b, c));//y是双精度
float z = stof(s[a].substr(b, c));//浮点