i/o file manipulation

This is a discussion on i/o file manipulation within the C++ Programming forums, part of the General Programming Boards category; I am creating a listed database of employees from an external file..it should read in the first txt file info ...

  1. #1
    Registered User mouse163's Avatar
    Join Date
    Dec 2002
    Posts
    49

    i/o file manipulation

    I am creating a listed database of employees from an external file..it should read in the first txt file info and create the database-
    and then generate a payday with a second file that has hours
    worked and sales....i am just going NUTS trying to pass the files around in this prgram. I get no errors - it just crashes and points to this same line in the <list> header:

    *_S++ = _Tr::to_char_type(_C);
    _CATCH_IO_(_I) }
    *_S = _E(0);<-------------------------------this one
    _I.width(0);
    _I.setstate(_S == _X ? _St | ios_base::failbit : _St);
    return (_I); }
    template<class _E, class _Tr> inline

    i figure something must be really wrong for this to pop up but,
    i am struggling to tune it down. Can anyone possiblly look and
    perhaps idenitfy and error or many?
    thanks so much

    Code:
    #include<iostream>
    using std::cin;
    using std::cout;
    using std::endl;
    using std::ios;
    using std::ostream;
    using std::istream;
    
    #include<list>
    
    
    #include<iomanip>
    using namespace std;
    
    #include <fstream>
    using std::ifstream;
    using std::ofstream;
    
    //Employee base class (struct Name, struct otherdata) with derived classes of salary, hourly and commission
    
    //___________________________________________________
    
    //Database class
    
    //#define NAME_MODE 1 // 
    
    typedef list < employee > EMPLOYEE_LIST;
    typedef EMPLOYEE_LIST::iterator EMPLOYEE_LISTiterator;
    
    class Employee_Database 
    	{
    	public:
    		
    		Employee_Database(char* filename); 
    		~Employee_Database(){}
    		
    		void Run( char* filename );
    		
    	private:
    		EMPLOYEE_LIST datalist;
    
    		EMPLOYEE_LISTiterator locateEmp (EmployeeData ED);
    	
    		char databaseFilename[256];
    		char dataInputFilename[256];
    		
    		void showMenu();
    		int getMenuSelection();
    		
    		void Hire();
    		void Fire();
    		void Retire();
    		
    		void PrintEmployees();
    		void outfileEmployees();
    		void LoadData();
    		void LoadData2();
    		void GetDataRecord(EmployeeData &newEmployee, istream& infile);
    		void UserInput( EmployeeData &ED);
    		int GetNextID();
    	};
    
    //database constructor
    Employee_Database::Employee_Database( char* filename)
    	{
    	
    	strcpy( databaseFilename, filename);
    	strcpy( dataInputFilename, " " );
    	datalist.clear();
    	
    	}
    
    void Employee_Database::Run (char* databaseFilename)
    	{										
    	strcpy( dataInputFilename, " " );//
    	
    
    	LoadData();
    	LoadData2();
    	
    	int more = 1;
    	
    	do {
    		showMenu();
    		
    		switch (getMenuSelection()) {
    			case 0:
    				more = 0;
    				break;
    				
    			case 1:
    				Hire();
    				PrintEmployees();
    				break;
    				
    			case 2:
    				Fire();
    				PrintEmployees();
    				break;
    				
    			case 3:
    				Retire();
    				PrintEmployees();
    				break;
    				
    			case 4:
    				PrintEmployees();
    				break;
    				
    			default:
    				cout << "Invalid selection" << endl;
    				break;
    			}
    		
    		if (more) {
    			cout << "<enter> to continue...";
    			cin.ignore();
    			cin.get();
    			}
    		
    		} while (more);
    	
    	}//end run
    
    
    void Employee_Database::showMenu()
    	{
    //	system( "cls" );
    	
    	cout << "Employee Database Menu\n";
    	
    	cout << "\n1) Hire Employee";
    	cout << "\n2) Fire Employee";
    	cout << "\n2) Retire Employee";
    	cout << "\n3) Print Employees";
    	cout << "\n [ 0 to quit ]";
    	cout << endl;
    	}
    
    
    
    
    int Employee_Database::getMenuSelection()
    	{
    	int select = 0;
    	cout << "Enter your selection: ";
    	cin >> select;
    	return select;
    	}
    
    
    
    
    void Employee_Database::UserInput( EmployeeData &ED)
    	{
    		cout << "Enter Employee Name (Last, First): ";
    		cin >> ED.name.last;
    		cin >> ED.name.first;
    		
    	cout << endl;
    	
    	}
    
    
    void Employee_Database::Hire()
    	{
    	EmployeeData newemployee;
    	
    	UserInput( newemployee );
    	
    	if ( strlen(newemployee.name.first) != 0 ) 
    		{
    		newemployee.id = GetNextID();
    		employee employeeObject( newemployee );
    		datalist.push_back( employeeObject );
    		}
    	
    	}
    
    
    
    
    void Employee_Database::Fire()
    	{
    	EmployeeData badEmployee;
    	
    	UserInput( badEmployee);
    	
    	EMPLOYEE_LISTiterator iter = locateEmp( badEmployee );
    	
    	if ( iter != 0 ) {
    		datalist.erase( iter );
    		}
    	}
    
    
    void Employee_Database::Retire()//need a way to differentiate this from fire()
    	{
    	
    	EmployeeData oldEmployee;
    	
    	UserInput( oldEmployee);
    	
    	EMPLOYEE_LISTiterator iter = locateEmp( oldEmployee );
    	
    	if ( iter != 0 ) {
    		datalist.erase( iter );
    		}
    	}
    
    
    void Employee_Database::PrintEmployees()
    	{
    	EMPLOYEE_LISTiterator iter;
    	
    	for ( iter = datalist.begin(); iter != datalist.end(); iter++ ) {
    		iter->print();
    		}
    	}
    
    void Employee_Database::outfileEmployees()
    {
    EMPLOYEE_LISTiterator iter;
    	
    	for ( iter = datalist.begin(); iter != datalist.end(); iter++ ) {
    		iter->outfile();
    		}
    }
    
    void Employee_Database::LoadData() 
    	{
    cout << "Opening File PRO_IN.txt\n\n";
    	  
        ifstream infile( "PRO_IN.txt" );
    
    	if ( infile.fail() )
    	{
    		cerr << "File could not be opened." << endl;
    		return;
    	}
    
    	
    	EmployeeData newEmployee;
    
    	cout << "EmployeeData newEmployee created"<<endl;
    
      while (!infile.eof())
      {
      cout << "In while Loop"<<endl;
    
      GetDataRecord (newEmployee, infile);
    
      cout << "GetDataRecord passed"<<endl; //GetDataRecord (newEmployee, infile)-this call fails
    
      employee EmployeeObject(newEmployee);
    
      datalist.push_back(EmployeeObject);
    	
    	}
    
    }//end LoadData
    
    
    void Employee_Database::LoadData2() 
    	{
    
    	cout << "Opening File PDy.txt.\n\n";
    	  
        ifstream infile2( "PDy.txt" );
    
    	if ( infile2.fail() )
    	{
    		cerr << "File could not be opened." << endl;
    		return;
    	}
    	
    		char* first = " ";
    		char* last = " ";
    
    	EmployeeData Employee1;
    
      while (!infile2.eof())
    {
      infile2 >> first >> last;
    
      if ((strcmp(Employee1.name.last, last)==0) &&
    		(strcmp(Employee1.name.first, first)==0))
    		{
    			GetDataRecord (Employee1, infile2);
    		}
    
    }
    
    	}//end LoadData2
    
    void Employee_Database::GetDataRecord( EmployeeData &newEmployee, istream& infile)
    	{
    		
    		
    		while (!infile.eof() )
    		{   
    			infile >>newEmployee.name.first
    					>>newEmployee.name.last
    					>>newEmployee.ssn
    					>>newEmployee.type
    					>>newEmployee.rate;
    				
    			}
    	
    		
    	}
    
    
    
    
    
    
    
    EMPLOYEE_LISTiterator Employee_Database::locateEmp( EmployeeData ED )
    	{
    	// Searches for exact match
    	// Returns the location via iterator
    	EMPLOYEE_LISTiterator iter;
    	
    	for (iter = datalist.begin(); iter != datalist.end(); iter++ ) 
    {
    		if ((iter->data.name.last == ED.name.last )&& (iter->data.name.first == ED.name.first ) ) {
    			break;
    			}
    		
    	
    		}
    			//     (condition)?  value-if-true : value-if-false;
    		return (iter == datalist.end())? iter = 0 : iter;
    	}
    
    int Employee_Database::GetNextID()
    	{
    	static id = 0;
    	return ++id;
    	}
    
    
    void main()
    	{
    	
    
        ofstream outfile("eDatabase.txt"); 
        if(!outfile){ 
            cerr << "Cannot open output file" << endl; 
            //exit(1); 
    		} 
    	
    	Employee_Database *E_DB = new Employee_Database( "EmpDB.txt" );
    
    	E_DB->Run( "PRO_IN.txt" );
    
    	delete E_DB;
    
    	cin.get();
    	}//main

  2. #2
    Registered User
    Join Date
    Jan 2002
    Posts
    552
    can you provide a stack trace of the point where the error occurs? Meaning, what functions are called that leads up to the line that causes the error.
    C Code. C Code Run. Run Code Run... Please!

    "Love is like a blackhole, you fall into it... then you get ripped apart"

  3. #3
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    #include<iostream>
    using std::cin;
    using std::cout;
    using std::endl;
    using std::ios;
    using std:stream;
    using std::istream;

    #include<list>


    #include<iomanip>
    using namespace std;

    #include <fstream>
    using std::ifstream;
    using std:fstream;


    I know some people advocate doing this, but don't you think it's a ridiculous amount of typing? In addition, you then include the line:

    using namespace std;

    which makes all the

    using std::

    statements redundant.

  4. #4
    Registered User mouse163's Avatar
    Join Date
    Dec 2002
    Posts
    49

    stack trace

    ClownPimp,
    thank for your interest - i am still kinda new to programming
    (if you want to define my struggles as that ;-D) how do i do a stack trace?

    M

  5. #5
    Registered User mouse163's Avatar
    Join Date
    Dec 2002
    Posts
    49

    header inclusives:

    ---there are certain instances where using namespace std; IS appropriate and others when it doen't hold water.
    In addition using namespace std; in the above referenced instance is relative to iomanip NOT iostream.
    For a senior member, I would think you would exemplify your knowlege with a comment a little more pertinent.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Binary File Manipulation Speeds
    By Shonumi in forum C++ Programming
    Replies: 5
    Last Post: 04-20-2009, 03:07 AM
  2. opening empty file causes access violation
    By trevordunstan in forum C Programming
    Replies: 10
    Last Post: 10-22-2008, 12:19 AM
  3. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 10:54 AM
  4. Need a suggestion on a school project..
    By Screwz Luse in forum C Programming
    Replies: 5
    Last Post: 11-27-2001, 02:58 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21