Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Reading text file

Posted on 2004-10-09
3
221 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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

809 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