阅读标签在C分离文件到阵列++ [英] Read tabs separated file into arrays in c++

查看:138
本文介绍了阅读标签在C分离文件到阵列++的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这样的文件:

INT1 - 标签 - INT2 - 标签 - INT3 - 标签 - INT4 - 选项卡 - 换行

INT1 - 标签 - INT2 - 标签 - INT3 - 标签 - INT4 - 选项卡 - 换行

INT1 - 标签 - INT2 - 标签 - INT3 - 标签 - INT4 - 选项卡 - 换行
...

我要救的每一行中的数组。我的意思是所有的INT1到一个数组,并希望做同样的白衣INT2 INT3 ...

我真的不知道该怎么做了,请帮助我

我已经尝试通过逐行阅读

 的#include< sstream>
#包括LT&;串GT;性病::串线;
而(的std ::函数getline(INFILE,线))
{
    的std :: istringstream ISS(线);
    诠释A,B;
    如果((ISS>!&GT a取代;> b))的{打破; }}


解决方案

您已经使用stringstream的想法是正确的。由于code读取分隔的文件很可能被再次使用,你可能会发现它有用投入到这一类。下面是从我个人的分隔的FileReader类的摘录:

 布尔的FileReader :: GETROW(RowMap&安培;排){
    性病::串线=;
    如果(的std ::函数getline(文件句柄,行)){
        性病:: stringstream的line_ss(线);
        性病::字符串列=;
        无符号整型指数= 0;
        而(的std ::函数getline(line_ss,列分隔符)){
            如果(指数< headers.size()){
                行[标题[指数] =列;
                指数++;
            }
            其他{
                打破;
            }
        }
        返回true;
    }
    返回false;
}

在哪里RowMap是一个typedef:

 的typedef的std :: unordered_map<的std ::字符串,性病::字符串>

和头是一个typedef:

 的typedef的std ::矢量<标准::字符串> RowHeadersVector;

和应该有你的列名:

  RowHeadersVector头;
headers.push_back(COLUMN_1);

在我的例子中,我使用的地图串来串的,但你可以很容易地将其更改为:

 的typedef的std :: unordered_map<的std ::字符串,整数>

使用地图像这样的好处是自我证明code:

 行[COLUMN_1]

I have file like this:

int1--tab--int2--tab--int3--tab--int4--tab--newline

int1--tab--int2--tab--int3--tab--int4--tab--newline

int1--tab--int2--tab--int3--tab--int4--tab--newline ...

I want to save each row in to an array. I mean all int1 in to an array and want to do the same whit int2 int3 ...

I realy dont know how to do it please help me

I already try read it line by line

#include <sstream>
#include <string>

std::string line;
while (std::getline(infile, line))
{
    std::istringstream iss(line);
    int a, b;
    if (!(iss >> a >> b)) { break; } 

}

解决方案

You had the right idea using a stringstream. Since code to read delimited files is likely to be used again, you may find it useful to put this into class. Here's an excerpt from my personal delimited FileReader class:

bool FileReader::getrow(RowMap &row){
    std::string line = "";
    if(std::getline(filehandle,line)){
        std::stringstream line_ss(line);
        std::string column = "";
        unsigned int index = 0;
        while(std::getline(line_ss,column,delimiter)){
            if(index < headers.size()){
                row[headers[index]] = column;
                index++;
            }
            else{
                break;
            }
        }
        return true;
    }
    return false;
}

Where RowMap is a typedef of:

typedef std::unordered_map<std::string,std::string>

And headers is a typedef of:

typedef std::vector<std::string> RowHeadersVector;

And should have your column names:

RowHeadersVector headers;
headers.push_back("column_1");

In my example, I'm using a map of string to string, but you could easily change it to:

typedef std::unordered_map<std::string,int>

The benefit of using a map like this is self documented code:

row["column_1"]

这篇关于阅读标签在C分离文件到阵列++的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆