Solved

Reading text file

Posted on 2004-10-09
3
212 Views
Last Modified: 2010-04-01
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
--
0
Comment
Question by:w100ktn
  • 2
3 Comments
 
LVL 44

Expert Comment

by:Karl Heinz Kremer
ID: 12268609
The EmpRec implementation file is missing.
0
 
LVL 17

Expert Comment

by:rstaveley
ID: 12269857
The trouble with using operator<< is that "Data Processing" will be parsed as two tokens.
0
 
LVL 17

Accepted Solution

by:
rstaveley earned 250 total points
ID: 12269858
Use getline()
0

Featured Post

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
how to understand recursion 12 226
Move constructor only called if marked noexcept? 6 104
Socket Programming (Unix) 8 127
object oriented programming comparison 5 70
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

776 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question