C++ 将txt文件数据指定给链表中的结构节点

C++ 将txt文件数据指定给链表中的结构节点,c++,linked-list,nodes,fstream,assign,C++,Linked List,Nodes,Fstream,Assign,好的,所以我以前从未使用过fstream,也没有在程序中打开和读取和文件。我的指导老师只给出了几行代码,可以打开、读取和关闭文本文件。我应该从文本文件中取出数据并将其放入链接列表中的单独节点中,然后继续对其执行其他不重要的操作,因为我知道如何执行。我的问题是,我不知道如何将这些值分配给结构值 txt文件如下所示: 克拉克肯特55000 2500 0.07 路易斯巷56000 1500 0.06 托尼·斯塔克34000 2000 0.05 我创建了一个名为Employee的结构,然后创建了基本的

好的,所以我以前从未使用过fstream,也没有在程序中打开和读取和文件。我的指导老师只给出了几行代码,可以打开、读取和关闭文本文件。我应该从文本文件中取出数据并将其放入链接列表中的单独节点中,然后继续对其执行其他不重要的操作,因为我知道如何执行。我的问题是,我不知道如何将这些值分配给结构值

txt文件如下所示:

克拉克肯特55000 2500 0.07

路易斯巷56000 1500 0.06

托尼·斯塔克34000 2000 0.05

我创建了一个名为Employee的结构,然后创建了基本的insert函数,以便可以向列表中添加新节点。现在我如何将这些名称和数字输入到我的结构中

这是我的密码:

#include <fstream>
#include <iostream>
using namespace std;

struct Employee
{
    string firstN;
    string lastN;
    float salary;
    float bonus;
    float deduction;

    Employee *link;
};

typedef Employee* EmployPtr;
void insertAtHead( EmployPtr&, string, string, float, float,float );
void insert( EmployPtr&, string, string, float, float,float );

int main()
{
    // Open file
    fstream in( "payroll.txt", ios::in );

    // Read and prints lines
    string first, last;
    float salary, bonus, deduction;

    while( in >> first >> last >> salary >> bonus >> deduction)
    {
        cout << "First, last, salary, bonus, ded: " << first << ", " << last << ", " << salary << ", " << bonus << ", " << deduction <<endl;
    }

    // Close file
    in.close();

    EmployPtr head = new Employee;


 }

void insertAtHead(EmployPtr& head, string firstValue, string lastValue,
            float salaryValue, float bonusValue,float deductionValue)
{
    EmployPtr tempPtr= new Employee;

    tempPtr->firstN = firstValue;
    tempPtr->lastN = lastValue;
    tempPtr->salary = salaryValue;
    tempPtr->bonus = bonusValue;
    tempPtr->deduction = deductionValue;

    tempPtr->link = head;
    head = tempPtr;
}

void insert(EmployPtr& afterNode, string firstValue, string lastValue,
        float salaryValue, float bonusValue,float deductionValue)
{
    EmployPtr tempPtr= new Employee;


    tempPtr->firstN = firstValue;
    tempPtr->lastN = lastValue;
    tempPtr->salary = salaryValue;
    tempPtr->bonus = bonusValue;
    tempPtr->deduction = deductionValue;

    tempPtr->link = afterNode->link;
    afterNode->link = tempPtr;
}
#包括
#包括
使用名称空间std;
结构雇员
{
先串;
字符串lastN;
浮动工资;
浮动奖金;
浮动扣除;
员工链接;
};
typedef Employee*EmployPtr;
void insertatead(EmployPtr&,string,string,float,float,float);
无效插入(EmployPtr&,string,string,float,float,float);
int main()
{
//打开文件
fstream-in(“payroll.txt”,ios::in);
//阅读并打印行
先串,后串;
浮动工资、奖金、扣减;
同时(在>>第一次>>最后一次>>工资>>奖金>>扣减中)
{
cout link=afterNode->link;
afterNode->link=temptr;
}
<>也,我已经尝试过搜索,结果已经出来了,但是它们都是打开的,并且读数据的方式不同于我所给出的。我对java的C++是新的,所以我不理解我有时看到的一些代码。< /P> <代码>
EmployPtr head = new Employee;

while( in >> first >> last >> salary >> bonus >> deduction)
{
    cout << "First, last, salary, bonus, ded: " << first << ", " << last << ", " << salary << ", " << bonus << ", " << deduction <<endl;
    insertAtHead (head, first, last, salary, bonus, deduction);
}
同时(在>>第一次>>最后一次>>工资>>奖金>>扣减中) {
你的讲师难道没有介绍如何实例化对象吗?是的,我知道如何实例化对象哈哈,我基本上不知道while循环中发生了什么。数据被格式化并打印出来,但我不确定这是如何发生的,我真的没有看到任何代码将文本数据分配给这些变量,首先,最后,工资等等请按原样打印。如果有意义的话?谢谢,成功了!我只是不知道这些值已经分配给了某个对象。