1. We have moved from vBulletin to XenForo and you are viewing the site in the middle of the move. Though the functional aspect of everything is working fine, we are still working on other changes including the new design on Xenforo.
    Dismiss Notice

Garbage Stored in file

Discussion in 'C++' started by bouki, Apr 19, 2011.

  1. bouki

    bouki New Member

    Joined:
    Apr 9, 2010
    Messages:
    14
    Likes Received:
    0
    Trophy Points:
    0
    Could anyone explain why garbage storing in the file that is created.
    When i try to display the information its the same rubbish coming on the screen.


    Code:
    #include <iostream>
    #include <fstream>
    using namespace std;
     
    // Declaration of OK_Photo structure
    struct OK_Photo
    {
    	char name[20];		        //Employee Full Name
    	char dept[20];	    	    //Department Employee works in
    	float overtime;	            //Overtime worked
    	float hrs;		            //Hours worked
    	float deductions;           // Salary Deductions
    	int EmpNum;                 // Employee Number
    };
     
    //Function Prototypes
    int menu();
    void AddRecord(fstream &);
    void DisplayRecord(fstream &);
    void ChangeRecord(fstream &);
    /*
    void Pay(fstream &);
     */
     
    int main()
    {
    	int choice;
    	fstream payroll ("payroll.txt", ios::app | ios::binary);
    	OK_Photo record = {" ", 0, 0.0};
     
    	//writing the blank records
    	for (int count = 0; count < 5; count++)
    	{
    		payroll.write(reinterpret_cast<char *>(&record), sizeof(record));
    	}
     
    	//inventory.read(reinterpret_cast<char *>(&record), sizeof(record));
     
    	payroll.close();
     
    	payroll.open("payroll.txt", ios::app | ios::binary);
     
    	do
    	{
    		choice = menu();
    		switch (choice)
    		{
    		case 1: AddRecord(payroll);
    			break;
    		case 2: DisplayRecord(payroll);
    			break;
    		case 3: ChangeRecord(payroll);
    			break;
    			/*
    		case 4: Pay(payroll);
    			break;
    		*/
    		case 5: cout << "Exiting Program...\n\n";
    		}
    	}while (choice != 5);
     
    	payroll.close();
    	return 0;
     
    }	
     
    int menu ()
    {
    	int selection;
     
    	cout<<"\n\n===== Main Menu ====="<<endl;
    	cout << "1) Add Record \n";
    	cout << "2) Display Record \n";
    	cout << "3) Update Record \n";
    	cout << "4) Pay \n";
    	cout << "5) Exit Program \n";
    	cout << "Please enter[1, 2, 3, 4 or 5]: ";
    	cin >> selection;
     
    	while (selection< 1 || selection > 5)
    	{
    		cout << "Invalid Choice!!\n";
    		cin >> selection;
    	}
    	return selection;
    }
     
    void ChangeRecord(fstream &file)
    {
    	fstream payroll("payroll.txt", ios::in);
    	OK_Photo record;
    	 
    	// Get the record number of the desired record.
    	cout << "\n\nEnter Employee Number for update: \n";
    	cin >> record.EmpNum;
     
    	// Move to the record and read it.
    	payroll.seekg(record.EmpNum * sizeof(record), ios::beg);
    	payroll.read(reinterpret_cast<char *>(&record),
    		           sizeof(record));
     
    	
    	// Display the record contents
    	cout << "\n\nEmployee Name: ";
    	cout << record.name << endl;
    	cout << "Department: ";
    	cout << record.dept << endl;
    	cout << "Hours Worked: ";
    	cout << record.hrs << endl;
    	cout << "Overtime: ";
    	cout << record.overtime << endl;
        cout << "Deductions :"; 
        cout << record.deductions <<endl;
        cout << "Total Salary :";
        cout << record.hrs + record.overtime - record.deductions;
     
    	// Get the new record data.
    	cout << "\n\nEmployee Number: ";
    	cin >> record.EmpNum;
    	
    		cout << "Employee Name: ";
    	cin.ignore();
    	cin.getline(record.name, 20);
    	
        	cout << "Department: ";
    	cin >> record.dept;
    	
    	    cout << "Hours Worked: ";
    	cin.ignore();
    	cin >> record.hrs;
    	
        	cout << "Overtime: ";
    	cin.ignore();
    	cin >> record.overtime;
    	
    	cout << "Total Deductions: ";
    	cin.ignore();
    	cin >> record.deductions;
     
    	// Move back to the beginning of this record's position.
    	payroll.seekp(record.EmpNum * sizeof(record), ios::beg);
     
    	// Write the new record over the current record.
    	payroll.write(reinterpret_cast< char *>(&record),
    		            sizeof(record));
    		            
     	     
     	// Close the file.
    	file.close();
    }
     
    void DisplayRecord(fstream &file)
    {
    	fstream payroll; 
    	OK_Photo record;
        payroll.open("payroll.txt", ios::in);
    	
    
    	// Get the record number of the desired record.
    	cout << "\n\nEmployee Number: ";
    	cin >> record.EmpNum;
     
    	// Move to the record and read it.
    	payroll.seekg(record.EmpNum * sizeof(record), ios::beg);
    	payroll.read(reinterpret_cast<char *>(&record),
    		           sizeof(record));
    
      	           
    cout<<"\n\n-----Displaying record for Employee " <<record.name<<endl;	 
    
    	// Display the record contents
    	cout << "\n\nEmployee Name: ";
    	cout << record.name << endl;
    	cout << "Department: ";
    	cout << record.dept << endl;
    	cout << "Hours Worked: ";
    	cout << record.hrs << endl;
    	cout << "Overtime: ";
    	cout << record.overtime << endl;
        cout << "Deductions :"; 
        cout << record.deductions <<endl;
        
    
    	//closing the file
    	file.close();
    }
    
    
     
    void AddRecord(fstream &file)
    {
    	cout << "\n\nPlease enter the information for the new data: \n";
     
    	ofstream payroll ("payroll.txt", ios::app);
    	OK_Photo record;
     
    	//Info of the new data
    	
    	cout << "\n\nEmployee Number: ";
    	cin >> record.EmpNum;
    	
    		cout << "Employee Name: ";
    	cin.ignore();
    	cin.getline(record.name, 31);
    	
        	cout << "Department: ";
    	cin >> record.dept;
    	
    	    cout << "Hours Worked: ";
    	cin.ignore();
    	cin >> record.hrs;
    	
        	cout << "Overtime: ";
    	cin.ignore();
    	cin >> record.overtime;
    	
    	cout << "Deductions: ";
    	cin.ignore();
    	cin >> record.deductions;
     
    	payroll.write(reinterpret_cast< const char *>(&record),
    		            sizeof(record));
     
    	//closing the file
    	file.close();
    }
     
  2. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,012
    Likes Received:
    203
    Trophy Points:
    0
    Occupation:
    Senior Support Engineer
    Location:
    England
    Probably undefined behaviour of some kind. As you're using fixed size arrays for strings check that you're not overflowing them, for example, this might:
    Code:
    		cout << "Employee Name: ";
    	cin.ignore();
    	cin.getline(record.name, 31);
    
    as record.name is defined as char[20], which gives you 19 characters for the name plus a terminating NULL.

    For any more detail you'd also have to post payroll.txt (NOT all of it - just enough to reproduce the problem) and whatever input you're giving the program. Also the output you get could be useful as it might point directly to the problem.
     

Share This Page