文件读取(C++)
- 空格为间隔,逐行循环读取
ifstream Camerafile;
Camerafile.open("C://Users/mac/Desktop/bundleadjustment_SCBA_Camera_Result.txt", ios::in);
if (Camerafile.is_open())
{
string s;
double d2, d3;
getline(Camerafile, s, '\n');
Camerafile >> xx0 >> yy0 >> d >> d2 >> d3 >> pixel >> k1 >> k2 >> p1 >> p2 >> α >> β;
Camerafile.close();
//*********************************
ifstream Pointfile;
Pointfile.open("C://Users/mac/Desktop/bundleadjustment_SCBA_Point_Result.txt", ios::in);
if (Pointfile.is_open())
{
string s;
//跳过两行
int j, k;
Pointfile >> j >> k;
getline(Pointfile, s, '\n');
//循环读取
while (!Pointfile.eof())
{
double a, b, c, d, e,f, m1, m2, n1, n2;
int num;
Pointfile >> a >> b >> c >> d >> e ; //空格间隔读取
Pointnum.push_back(a);
X.push_back(b);
Y.push_back(c);
Z.push_back(d);
}
if (photofile.is_open())
{
string a, b;
//跳过第一行
getline(photofile, b, '\n');
getline(photofile, a);
if (count == 2) getline(photofile, a);
//将string转为char*
//指向首地址
char *s_input = (char *)a.c_str();
const char * split = "\t"; //split表示间隔符
char *buf;
char *p = strtok_s(s_input, split, &buf);
double num;
vector<double> element;
while (p != NULL)
{
//num = atof(p);
sscanf_s(p, "%lf", &num);
element.push_back(num);
p = strtok_s(NULL, split, &buf);
}