sstream读取文件

对于如下图所示的数据文件:
在这里插入图片描述

274表示有274个点对,以下每一行代表一个点对,每一行的四个数从左到右依次是一个第一个点的x坐标、y坐标、第二个点的x坐标、y坐标,现在要把点对数和每个点对读取并存储,具体代码如下:

#include<iostream>
#include<sstream>
#include<fstream>
#include<string>
#include<vector>
#include<assert.h>
using namespace std;


struct Correspondence2D2D
{
    double p1[2];
    double p2[2];
};
typedef vector<Correspondence2D2D> Correspondences2D2D;

int main(){
	Correspondences2D2D corr_all;
	int n=0;
	ifstream in("correspondences.txt");
	assert(in.is_open());
	string line, word;
    int n_line = 0;
	while(getline(in, line)){
    	stringstream stream(line); //等价于stringstream stream; stream<<line; 向流中传值
        if(n_line==0){
            int n_corrs = 0;
            stream>> n_corrs; //将流中的值读取到n_corrs中
            corr_all.resize(n_corrs);
			cout<<"Total line is "<<n_corrs<<endl;
            n_line ++;
            continue;
        }
        if(n_line>0){

            stream>>corr_all[n_line-1].p1[0]>>corr_all[n_line-1].p1[1]
                  >>corr_all[n_line-1].p2[0]>>corr_all[n_line-1].p2[1];
            cout<<n_line<<" line is "<<corr_all[n_line-1].p1[0]<<" "<<corr_all[n_line-1].p1[1]<<" "
                  <<corr_all[n_line-1].p2[0]<<" "<<corr_all[n_line-1].p2[1]<<endl;
        }
        n_line++;
    }
	return 0;
}

运行结果如下:
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值