繁体   English   中英

从C ++中的文本文件填充结构

[英]filling a struct from a text file in C++

伙计们,我有一个文本“ sales.txt”文件,其中包含以下格式的(id,姓氏,季度,销售)。

123 smith 1 333.20
221 doe 1 345.50
342 johnson 1 774.50
123 smith 2 333.20
221 doe 2 555.50
342 johnson 2 25.50
123 smith 3 254.20
221 doe 3 652.50
342 johnson 3 32.50
123 smith 4 354.20
221 doe 4 51.50
342 johnson 4 1000.50

我试图将文件放入一个结构中以输出到另一个文本文件,但是到目前为止,我在提取“ id”和姓氏时遇到了问题。 这是代码的一部分。 第二种方法是按季度提取销售并排列成阵列,但如果有人可以同时使用这两种方法,那将不起作用

谢谢

#include <iostream>
#include <string>
#include <fstream>

using namespace std;

struct employees { int id; string lname; double qtrSale[4]; double tsale; }; 
void getIdName(employees list[], ifstream& infile, int num);

int main()
{

ifstream infile;
    string file("file1.txt");

    infile.open(file);
    employees list[lsize];
    getIdName(list, infile, lsize);
    /*getData(list, file, lsize);*/

    for(int i = 0; i < lsize; i++)//checking struct
    {
        cout << list[i].id << " " << list[i].lname << endl;
    }
}

void getIdName(employees list[], ifstream& infile, int lsize)
{
    int id; 
    string lname; 
    double sale, temp;

    for(int i = 0; i < lsize; i++)
    {
        infile >> list[i].id >> list[i].lname >> temp >> sale;
        /*for(int j = 1; j <= 4; j++)
        {
            list[i].qtrSale[j] = 0.0;
        }*/

    }
}

void getData(employees list[], string file, int lsize)
{
    int id, qtr; 
    double amount;
    ifstream infile(file);
    while(infile.good())
    {
        for(int j = 0; j < lsize; j++)
        {
            infile >> id;
            for(int i = 1; i <= 4; i++)
            {
                infile >> qtr >> amount;
                if(list[j].id == id && qtr == 1) { list[j].qtrSale[i] = amount; }
                if(list[j].id == id && qtr == 2) { list[j].qtrSale[i] = amount; }
                if(list[j].id == id && qtr == 3) { list[j].qtrSale[i] = amount; }
                if(list[j].id == id && qtr == 4) { list[j].qtrSale[i] = amount; }
            }
        }
    }
}

学习标准的C ++库。 魔术在<iterator>中的std::istream_iterator<> ,为employee重载operator>>()并使用std::vector<>

#include<iostream>
#include<fstream>
#include<iterator>
#include<string>
#include<vector>

struct employee {
    int id;
    std::string lastname;
    int quarter;
    double sales;
};

template<class Ch, class Tr>
std::basic_istream<Ch,Tr>& operator>>(std::basic_istream<Ch,Tr>& in, employee& e)
{
    return in >> e.id >> e.lastname >> e.quarter >> e.sales;
}

int main(int argc, char* argv[])
{
    std::ifstream infile("sales.txt");
    std::vector<employee> employees((std::istream_iterator<employee>(infile)),
                                    std::istream_iterator<employee>());
    return 0;
}

我猜注释掉的部分不起作用? 在C ++中,数组从0开始索引,因此应改写该部分

for(int j=0; j < 4; j++)
{
    list[i].qtrSale[j] = 0.0;
}

您可以使用sscanf函数来解析数据。 遍历文件并一次抓取一行,然后按如下方式解析该行;

`

    char line []="123 smith 1 333.20" //here for example only
    char strname [20] = { 0 };
    int id = 0;
    int quarter = 0;
    double sales = 0.0;

    sscanf (line,"%d %s %d %f", &id, strname, &quarter, &sales);`

如果您不介意使用boost.spirit 这是完成的方式(在vc ++ 2010中测试):

#include <boost/spirit/include/qi.hpp>
#include <boost/fusion/include/adapt_struct.hpp>
#include <boost/spirit/include/support_istream_iterator.hpp>

#include <string>
#include <vector>
#include <iostream>
#include <fstream>

// step 1, define your struct
struct employee
{
    int id;  
    std::string lname; 
    int qtrSale; 
    double tsale; 
};

// step 2, adapt it
BOOST_FUSION_ADAPT_STRUCT(
    employee,
    (int, id)  
    (std::string, lname) 
    (int, qtrSale) 
    (double, tsale) 
)

// step 3, parse it
void parse_file()
{
    using namespace boost::spirit;

    // open file, disable skipping of whitespace
    std::ifstream in("sales.txt");
    in.unsetf(std::ios::skipws);

    std::vector<employee> ve;

    qi::phrase_parse
    (
        // parse from
        istream_iterator(in), 
        // parse to
        istream_iterator(),   
        // reads: an int followed by a ascii string, then by a double and finally by a double, repeatedly at least once.
        +(qi::int_ >> +qi::alpha >> qi::int_ >> qi::double_), 
        // skip all spaces
        qi::ascii::space, 
        // save results to ve
        ve 
    );

    // print the results out
    for(int i = 0; i< ve.size(); i++)
    { 
        std::cout 
             << ve[i].id        << " " 
             << ve[i].lname     << " "   
             << ve[i].qtrSale   << " "    
             << ve[i].tsale     << std::endl; 
    }    
}
    int main()
    {
        parse_file(); 
    }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM