文件读取(C++)

本文介绍了如何使用C++的ifstream读取并逐行处理`Camerafile`和`Pointfile`中的数据,包括字符串和数值,展示了如何跳过特定行并使用`getline`和`>>`操作进行空格分隔读取。
摘要由CSDN通过智能技术生成

文件读取(C++)

  1. 空格为间隔,逐行循环读取
 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);
  }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值