C++读取逗号分割的.txt文件

txt文件示例:

读取该文件,并存入一个vector之中:

        std::vector<cv::Point2f> featruePoint;
        featruePoint.clear();
        std::string seqname = fileList_image.substr(0, fileList_image.size() - 4);//fileList_image是txt文件对应的同名.png图像,使用                                                                                                                                           substr()取fileList_image的开始到倒数第四个字符之                                                                                                                                         间的字符。
        std::string pointfile = filedir_point + seqname + ".txt";//完整的txt文件所在的路径和名称
        std::ifstream infile;
        infile.open(pointfile.c_str());
        assert(infile.is_open());

        std::string s;
        while (getline(infile, s))
        {
            std::cout << s << std::endl;
            char c = s.at(0);
            if (c<'0' || c>'9')
                continue;
            std::stringstream ss;
            ss << s;
            float tmpd;
            int cnt = 0;
            float data[2];
            while (ss >> tmpd)
            {
                data[cnt] = tmpd;
                cnt++;
                if (cnt == 2)
                    break;
                if (ss.peek() == ',' || ss.peek() == ' ')
                    ss.ignore();
            }
            //std::cout << data[0] << std::endl;
            //std::cout << data[1] << std::endl;//逗号分割开的两个值
            featruePoint.push_back(cv::Point2f(data[0], data[1]));
        }
        infile.close();

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值