如何从具有以"|"分隔的字段的行中提取数据C ++中的字符? [英] How to extract data from a line which has fields separated by '|' character in C++?

查看:61
本文介绍了如何从具有以"|"分隔的字段的行中提取数据C ++中的字符?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在文本文件中具有以下格式的数据. 文件名-empdata.txt 请注意,两行之间没有空格.

I have data in the following format in a text file. Filename - empdata.txt Note that there are no blank space between the lines.

Sl | EmployeeID |名称|部门|乐队|位置

Sl|EmployeeID|Name|Department|Band|Location

1 | 327427 | Brock Mcneil |研究与开发| U2 | Pune

1|327427|Brock Mcneil|Research and Development|U2|Pune

2 | 310456 | Acton Golden |广告| P3 |海得拉巴

2|310456|Acton Golden|Advertising|P3|Hyderabad

3 | 305540 |霍莉·卡马乔|工资单| U3 |班加罗尔

3|305540|Hollee Camacho|Payroll|U3|Bangalore

4 | 218801 | Simone Myers |公共关系| U3 |浦那

4|218801|Simone Myers|Public Relations|U3|Pune

5 | 144051 |伊顿·本森|广告| P1 |金奈

5|144051|Eaton Benson|Advertising|P1|Chennai

我有一个像这样的班

class empdata
{
public:
int sl,empNO;
char name[20],department[20],band[3],location[20];
};

我创建了一个empdata类的对象数组. 如何从具有上述指定格式的n行数据的文件中读取数据并将其存储到创建的(类)对象数组中?

I created an array of objects of class empdata. How to read the data from the file which has n lines of data in the above specified format and store them to the array of (class)objects created?

这是我的代码

int main () {
string line;
ifstream myfile ("empdata.txt");
for(int i=0;i<10;i++) //processing only first 10 lines of the file
{
    getline (myfile,line);
    //What should I do with this "line" so that I can extract data 
    //from this line and store it in the class object?             

}

  return 0;
}

所以基本上我的问题是如何从字符串中提取数据,该字符串的数据用'|'分隔字符并将每个数据存储到单独的变量

推荐答案

我更喜欢使用 String Toolkit .字符串工具包将负责在解析时转换数字.

I prefer to use the String Toolkit. The String Toolkit will take care of converting the numbers as it parses.

这是我要解决的方法.

#include <fstream>
#include <strtk.hpp>   // http://www.partow.net/programming/strtk

using namespace std;

// using strings instead of character arrays
class Employee
{
    public:
    int index;
    int employee_number;
    std::string name;
    std::string department;
    std::string band;
    std::string location;
};


std::string filename("empdata.txt");

// assuming the file is text
std::fstream fs;
fs.open(filename.c_str(), std::ios::in);

if(fs.fail())  return false;   

const char *whitespace    = " \t\r\n\f";

const char *delimiter    = "|";

std::vector<Employee> employee_data;

// process each line in turn
while( std::getline(fs, line ) )
{

// removing leading and trailing whitespace
// can prevent parsing problemsfrom different line endings.

    strtk::remove_leading_trailing(whitespace, line);


    // strtk::parse combines multiple delimeters in these cases

    Employee e;

    if( strtk::parse(line, delimiter, e.index, e.employee_number, e.name, e.department, e.band, e.location) )
    {
         std::cout << "succeed" << std::endl;
     employee_data.push_back( e );
    }

}

这篇关于如何从具有以"|"分隔的字段的行中提取数据C ++中的字符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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