Assign txt file data to struct node in linked list

2019-06-05 02:29发布

问题:

Ok so I have never worked with fstream before or opened and read and files in a program. My instructor just gave a few lines of code that open, read, and close a text file. I'm supposed to take the data out of the text file and put it into separate nodes in a linked list and then go on to do other things with it which is not important because I know how to do it. My problem is that I don't know how to a assign these values to the struct values.

The txt file looks like this:

Clark Kent 55000 2500 0.07

Lois Lane 56000 1500 0.06

Tony Stark 34000 2000 0.05

I have created a structure called Employee and then basic insert functions so I can add new nodes to the list. Now how do I get these names and numbers into my structure.

here is my code:

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

Also, I have tried searching this and results have came up but they all opened and read the data differently than what i was given. I am new to c++ coming from java so I do not understand some of the code that I see sometimes.

回答1:

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

You already had 99% of the solution. You just have to construct the list as you read the file.