在C ++中用文本文件填充结构

时间:2011-02-22 01:52:41

标签: c++

伙计们,我有一个文本“sales.txt”文件,其中包含以下格式的(id,lastname,quarter,sales)。

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”和lastname的问题。这是代码的一部分。第二种方法是按季度提取销售并将其放入数组中,但是如果有人可以帮助我使用这两种方法,则无法正常工作

感谢

#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; }
            }
        }
    }
}

4 个答案:

答案 0 :(得分:3)

研究标准C ++库。魔法来自std::istream_iterator<> <iterator>operator>>()重载employee并使用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;
}

答案 1 :(得分:0)

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

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

答案 2 :(得分: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);`

答案 3 :(得分:0)

如果您不介意使用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(); 
    }