c++读取文件到vector

读取一个pts文件到一个vector<Point2f>里面。

其中pts文件如下:

version: 1
n_points: 68
{
266 246
264 279
269 312
274 345
283 378
302 407
328 429
365 447
399 452
434 445
466 432
494 410
512 383
522 352
525 322
530 287
536 256
288 215
304 199
327 191
349 195
370 202
415 199
436 193
460 191
481 194
496 209
395 239
395 260
396 278
396 296
367 320
378 322
394 323
410 321
421 319
311 242
328 230
349 234
360 246
348 245
331 245
433 244
445 231
465 229
484 241
464 243
448 244
342 356
362 344
383 338
399 338
412 339
432 344
449 356
434 373
414 384
398 388
381 386
362 376
359 359
383 355
397 356
412 355
436 359
413 365
397 368
383 367
}

 

 

#include <iostream>
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include <stdio.h>
#include <stdlib.h>
#include <vector>
#include <fstream>
#include<iostream>
#include <sstream>
#include <vector>
#include <string>
#include <fstream>
#include <iostream>

#ifdef __APPLE__
#include <sys/uio.h>
#else
#include <sys/io.h>
#endif



using namespace cv;
using namespace std;




vector<Point2f> readpts(){
    ifstream myfile("1.pts");
    string temp;
    if (!myfile.is_open())
    {
        cout << "未成功打开文件" << endl;
    }
    vector<Point2f> vp2f;
    int index=0;
    while(getline(myfile,temp)) {
         if((index>=3)&&(index<71)){
             Point2f p1;
             int a[2]={0};
             istringstream iss;//istringstream提供读 string 的功能
             iss.str(temp);//将 string 类型的 test 复制给 iss,返回 void
             string s;
             int i=0;
             while (iss >> s){
                  int x = stoi(s);
//                  cout << x << endl;//按空格读取string
                 a[i]=x;
                 i++;
             }
             p1.x=a[0];
             p1.y=a[1];
             vp2f.push_back(p1);
         }
        index++;
       
    }
    
    return vp2f;
}

int main(){

    Mat img=imread("1.jpg");

  vector<Point2f> landmark= readpts();
   
    for (int idx = 0; idx < landmark.size(); idx++) {
        cv::circle(img, landmark[idx], 1, cv::Scalar(0, 255, 0), -1);
    }
    imshow("img",img);
    cvWaitKey(0);
    return 1;
}

 

要将txt文件读取vector中,首先需要按照以下步骤进行操作: 1. 打开txt文件:可以使用C++的fstream库中的ifstream类来实现。首先创建一个ifstream对象,然后使用对象的open()函数将文件路径作为参数打开txt文件。 2. 检查文件是否成功打开:在打开文件后,我们需要检查文件是否成功打开。可以使用对象的is_open()函数来判断文件是否打开成功。如果文件成功打开,则可以继续进行读取操作;否则,需要返回错误信息。 3. 读取文件内容并存储到vector中:我们可以使用C++vector容器来存储读取到的txt内容。首先创建一个vector对象,然后使用一个循环来逐行读取文件内容。 4. 关闭文件:当读取文件内容后,需要关闭文件。可以使用ifstream对象的close()函数来关闭文件。 以下是一个示例代码: ```cpp #include <iostream> #include <fstream> #include <vector> #include <string> int main() { std::ifstream file; std::string line; std::vector<std::string> content; file.open("example.txt"); if (!file.is_open()) { std::cerr << "无法打开文件!" << std::endl; return 1; } while (std::getline(file, line)) { content.push_back(line); } file.close(); // 输出vector中的内容 for (const auto& str : content) { std::cout << str << std::endl; } return 0; } ``` 以上代码以读取名为"example.txt"的txt文件为例,将文件内容逐行读取并存储到名为content的vector中。最后通过循环输出vector中的内容。根据实际情况,你可以修改文件路径和输出方式。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值