Solved

Reading text file

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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
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.

743 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now