讀取文本資料2.0,讀取文本2.0

來源:互聯網
上載者:User

讀取文本資料2.0,讀取文本2.0
Introduction

In order to handle more complicated situation when we read data from a text file, I decided to write a new version, namely 2.0, of “reading data from text file”.

The code

Put the code firstly :)

code segments 1 : the headers your have to include and namepace you have to use.
#include <iostream>#include <vector>#include <fstream>#include <sstream>#include <string>using namespace std;#include <boost/algorithm/string.hpp>using namespace boost::algorithm;
Code segments 2: line checker

Line checker is used to check whether the given line contains specified number of values or not.

Firt parameter is the string includes values. The second is the specified or desired number of values in the previous string.

Because we usually desire the number of values in each txt line is equivalent. For instance :

Valid case showing below

1 2 34 5 6

and invalid case

1 23 4 5

And the code :

template<typename DT>bool check_line_data(  const string &str,    const int num_cols){    istringstream in_str(str);    int num_ele = 0;    DT tp_val;    while(in_str >> tp_val){        num_ele++;    }    return (num_ele==num_cols);}
Code segments 3: reading !

The Code !

template<typename DT>int read_from_txt(  vector<DT> &data,    int &num_rows, int &num_cols,     const string &filename){    ifstream in_file;     istringstream in_str;    string line; // the string to store one line.    DT tp_var;    int k;    in_file.open(filename.c_str(), ios_base::in);    if (!in_file.is_open()){        cerr << "failed to open the file of <" << filename << ">" << endl;        data.clear();        num_rows = 0;        num_cols = 0;        return 1;    }    // read the first line with content and check number of column.    num_cols = 0;    while (std::getline(in_file, line)){        trim(line);        if (line.size()==0)            continue;        in_str.str(line);        in_str.clear();        while(in_str >> tp_var) {            num_cols++;        }        break;    }    if (0==num_cols){ // empty file        num_rows = 0;        data.clear();        return 1;    }    // read the remaining data and stored into a vector.    in_file.seekg(ifstream::beg);    num_rows = 0;    while(std::getline(in_file, line))    {        // remove black characters from the begining and ending of the line.        // the only function depends on BOOST!!!!        trim(line);        if (line.size()==0)            continue;        in_str.clear();        in_str.str(line); // ready to read the data.        if (check_line_data<DT>(line, num_cols)){            for (k = 0; k < num_cols; k++){                in_str >> tp_var;                data.push_back(tp_var);            }            num_rows++;        }        else{            data.clear();            num_rows = 0;            num_cols = 0;            return 2; // differet number of lines        }    }    in_file.close();    return 0;}

vector<DT> &data : used to store the reading result in a row-major order.

int &num_rows, int &num_cols : used to retrieve number of lines and columns the text file contains.

const string &filename : the file name!

Go To Evaluate

Just Call the Last Function with correct arguments!!!

Conclustion

After serveral test case, what I think is right is the code is more robust than the first version.

It shows that the code has the ability to handle empty file(without numerical lines), invalid filename, file with empty lines, file with different number of columns, etc.

Note : the case below is considered as valid.

1 2 a3 4

And its output is :

1 2 3 4

If you have interest, please test the code~~

Enjoy it and good luck !!!!

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.