Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
194 views
in Technique[技术] by (71.8m points)

c++ - How to store lines from a file as a variable

I'd like to show the employee number name, occupation, and department of employees from a text file called organisation.txt, and save them in the variables declared in the class OrganisationRecord.

How can I do that?

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

#define ORGANISATIONALRECORDSFILE "organisation.txt"
#define HRRECORDSFILE "HR_records.txt"
#define PAYROLLRECORDSFILE "payroll_records.txt"

using namespace std;


class OrganisationRecord
{
private:
public:
    string name;
    string occupation;
    string department;
};

class HRRecord
{
private:
public:
    string address;
    string phonenumber;
    string ninumber;
};

class PayrollRecord
{
private:
public:
    string ninumber;
    double salary;
};

class PayrollProcessing
{
private:
    ifstream inputfile;
    ofstream outputfile;
    vector<OrganisationRecord> OrganisationRecords;
    vector<HRRecord> HRRecords;
    vector<PayrollRecord> PayrollRecords;
public:
    void loadOrganisationRecords(string filename);
    void loadHRRecords(string filename);
    void loadPayrollRecords(string filename);
    void displayEmployeeOfSalaryGTE(double salary);
    //GTE = greater than or equal to
};

void PayrollProcessing::loadOrganisationRecords(string filename)
{
    inputfile.open(ORGANISATIONALRECORDSFILE);

    if (!inputfile)
    {
        cout << "the organisation records file does not exist" << endl;
        return;
    }

        OrganisationRecord _organisationrecord;
        int employeenumber;


        while (inputfile >> employeenumber)
        {   
            while (inputfile >> _organisationrecord.name)
            {
                cout << _organisationrecord.name;
                cout << _organisationrecord.occupation;
                cout << _organisationrecord.department <<endl;
            }

            OrganisationRecords.push_back(_organisationrecord);
        }

}



int main(void)
{
    PayrollProcessing database1;
    database1.loadOrganisationRecords(ORGANISATIONALRECORDSFILE);

    return 0;
}

organisation.txt

0001 
Stephen Jones 
Sales Clerk 
Sales
0002 
John Smith 
Programmer 
OS Development
0003 
Fred Blogs 
Project Manager 
Outsourcing
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

When you use inputfile >> _organisationrecord.name it stops at the first whitespace character. Hence, only Stephen will be read and stored in organisationrecord.name.

You need to change your strategy a bit.

  1. Read the contents of the file line by line. Stop when there are no more lines left.
  2. Process each line as you seem fit.

Here's one way to deal with the input.

std::string line;
while ( std::getline(inputfile, line) )
{   
   // Extract the employeenumber from the line
   std::istringstream str(line);
   if ( !(str >> employeenumber) )
   {
      // Problem reading the employeenumber.
      // Stop reading.
      break;
   }

   if (!std::getline(inputfile, line) )
   {
      // Problem reading the next line.
      // Stop reading.
      break;
   }

   _organisationrecord.name = line;

   if (!std::getline(inputfile, line) )
   {
      // Problem reading the next line.
      // Stop reading.
      break;
   }
   _organisationrecord.occupation = line;

   if (!std::getline(inputfile, line) )
   {
      // Problem reading the next line.
      // Stop reading.
      break;
   }
   _organisationrecord.department = line;

   std::cout << _organisationrecord.employeenumber << std::endl;
   std::cout << _organisationrecord.name << std::endl;
   std::cout << _organisationrecord.occupation << std::endl;
   std::cout << _organisationrecord.department << endl;

   OrganisationRecords.push_back(_organisationrecord);
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...