读取文本数据2.1

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 3
4 5 6

and invalid case

1 2
3 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;
    }
    data.clear();
    // 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 a
3 4

And its output is :

1 2 
3 4

If you have interest, please test the code~~

Enjoy it and good luck !!!!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值