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

标签 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++ 库。魔法就在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;
}

关于c++ - 从 C++ 中的文本文件填充结构,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5073260/

相关文章:

java - 从构造函数中抛出未处理的异常时会发生什么

c++ - 在 C 程序中调用 C++ 共享库...如何管理?

c++ - 未定义的引用 - portaudio

c++ - boost +CMake : no suitable build variant

c++ - 如何记录 C++ 重复迭代函数的总时间?

c# - shell 扩展如何将多个选定的文件传递给我自己的程序?

c++ - g++ 编译器中的 “Uninitialized use” 警告

C++ - 具有返回模板类型的方法的模板类

c++ - 如何使用 MPI 在不同的处理器上使用相同的数组

c++ - SSE 中的条件结构