Link to home
Start Free TrialLog in
Avatar of w100ktn
w100ktn

asked on

Reading text file

Hi all,

I'm trying to read a simple text file to a dynamic array(self built class) of employee records (self built class) but have not succeeded. As I debug, the program didn't read anything from the file to the temporary variables (not touching any class here).

I've tried the sample of some of the solution posted here but they didn't help. I'd really appreciate that if any of you experts can help me make this program work.

I'm attaching the code of these files.


/*main.cpp*/
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
#include <iostream>
#include "EmpRec.h"
#include "Array.h"*/

void main()
{
      Array a;
      a.Read ("database.txt");
      a.Print ();
}


/*EmpREc.h*/

#include <stdlib.h>
#include <string.h>
#ifndef EmpRec_H_INCLUDE_GUARD
#define EmpRec_H_INCLUDE_GUARD


class EmpRec
{
      private:
            char *LastName;
            char *FirstName;
            char *EmpID;
            long Salary;
            char *Dept;
            char *PhoneNo;
            char *OfficeNo;
            char *HireDate;
            char *EmailAdd;
      public:
      EmpRec (const char *lname = NULL, const char *fname =NULL, const char *ID=NULL, const long EmpSalary =0, const char *department =NULL, const char *phone =NULL, const char *office =NULL, const char *date =NULL, const char *email =NULL)
      {
            strcpy (LastName, lname);
            strcpy (FirstName, fname);
            strcpy (EmpID, ID);
            Salary = EmpSalary;
            strcpy (Dept, department);
            strcpy (PhoneNo, phone);
            strcpy (OfficeNo, office);
            strcpy (HireDate, date);
            strcpy (EmailAdd, email);
      }
      void GetRecord(EmpRec * Node);
      void PrintRecord();
      char * getLastName();
      char * getFirstName();
      char * getEmpID();
      long getSalary();
      char *getDept();
      char *getPhoneNo();
      char *getOfficeNo();
      char *getHireDate();
      char * getEmailAdd();
      void DeleteRec();
};

#endif

/*Array.h*/
#include <stdio.h>
#include "EmpRec.h"

class Array
{
private:
      int size;
      EmpRec *listArray;
public:
      Array(int n);
      Array(){size =0,listArray=NULL;}
      ~Array() {delete [] listArray;}
      void Read(char *filename);
      void Save( char *filename);
      void Delete(int position);
      void Print();
};

/*Array.cpp*/
#include <stdlib.h>
#include <stdio.h>
#include <fstream.h>
#include "Array.h"
#include "EmpRec.h"
#include <iostream>


void Array::Read (char *filename)
{
      char *crap = NULL;
      char * lastname =NULL;
      char *firstname =NULL;
      char *ID =NULL;
      long salary =0;
      char *department =NULL;
      char *phone =NULL;
      char *office =NULL;
      char *date =NULL;
      char *email =NULL;
      ifstream filein;

      filein.open(filename, ios::in);
      if ( !filein.fail() )
      {
            size =0;
            while (!filein.eof())
            {
                  filein >> lastname;  //nothing read to lastname...
                  filein >> firstname;
                  filein >> ID;
                  filein >> salary;
                  filein >> department;
                  filein >> phone;
                  filein >> office;
                  filein >> date;
                  filein >> email;
                                                filein >> crap;
            }
            listArray[size++] = EmpRec(lastname, firstname, ID, salary, department, phone, office, date, email);
            filein.close();
      }
      else
            cerr << "ERROR: Cannot open file\r\n";
}

void Array::Print()
{
      int i=0;
      if (size==0)
            cout << "Array is empty";
      else
            for (i=0; i<size; i++)
                  cout << listArray[i].getLastName();
}

Array::Array(int n)
{
      size = n;
      listArray = new EmpRec[n];
}

The text file looks like:


Doe
John
523921
87000
Data Processing
555-1212
302 Russ
10/27/2004
JohnDoe@somewhere.com
--
Smith
Jane
239182
102000
Management
555-3022
128 Admin Bldg
03/12/1942
JaneSmith@salsamail.com
--
Johnson
Michael
387721
76000
Sales
555-3289
395 Sales Bldg
08/12/01
MichaelJohnson@mymail.com
--
Anderson
Bill
768323
82000
Marketing
555-9089
304 Sales Bldg
Billy@billymail.com
--
Powers
Susan
662312
105000
Legal
555-0022
100 Legal Street
SPowers@lawyers.com
--
Avatar of Karl Heinz Kremer
Karl Heinz Kremer
Flag of United States of America image

The EmpRec implementation file is missing.
The trouble with using operator<< is that "Data Processing" will be parsed as two tokens.
ASKER CERTIFIED SOLUTION
Avatar of rstaveley
rstaveley
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial