C/C++使用ifstream从文件中一行一行读取数据

一、概述

  案例:使用ifstream从文件中一行一行读取数据,并对数据进行分割

#include <fstream>
#include <string>
#include <stdio.h>
#include <stdlib.h>

  

二、代码示例

string filename = string("/Users/yangwei/Documents/tony/opencv/orl_faces/targetData.txt");
    ifstream file(filename,ifstream::in);
    string line,path,classLabel;//行、路径、标签
    vector<Mat> images;
    vector<int> labels;
    while(getline(file,line)){

        stringstream liness(line);
        getline(liness,path,' ');
        getline(liness,classLabel);
        //        if (!path.empty() && !labels.empty()) {
        cout << "path :"<< classLabel.c_str()<<endl;;
        images.push_back(imread(path, 0));
        labels.push_back(atoi(classLabel.c_str()));
        //        }
    }
    file.close();

 

posted on 2022-05-07 09:57  飘杨......  阅读(1838)  评论(0编辑  收藏  举报