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();

原文链接: https://www.cnblogs.com/tony-yang-flutter/p/16241268.html

欢迎关注

微信关注下方公众号,第一时间获取干货硬货;公众号内回复【pdf】免费获取数百本计算机经典书籍

原创文章受到原创版权保护。转载请注明出处:https://www.ccppcoding.com/archives/189059

非原创文章文中已经注明原地址,如有侵权,联系删除

关注公众号【高性能架构探索】,第一时间获取最新文章

转载文章受原作者版权保护。转载请注明原作者出处!

(0)
上一篇 2023年2月12日 下午2:36
下一篇 2023年2月12日 下午2:36

相关推荐