Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Reading text file

Posted on 2004-10-09
3
Medium Priority
?
259 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
[X]
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
  • 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 750 total points
ID: 12269858
Use getline()
0

Featured Post

Ask an Anonymous Question!

Don't feel intimidated by what you don't know. Ask your question anonymously. It's easy! Learn more and upgrade.

Question has a verified solution.

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

IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
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 goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
Suggested Courses

610 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