A simple question here. Working on a payroll project at school (my final project this semester) and I need some advice.
I have a payroll program with multiple options (add an employee record, display a record, modify a record, etc). This involves storing the employee data in both arrays and in an external file.
My final function I need to write so I can turn this in is probably a simple one, but I just don't know how to do it. Delete an employee record. Here's the delete record function:
void deleteRecord(){//function to select and delete a record
string empLastName[4];
string empName;
char payrollType[4];
int hoursWorked[4];
int payRate[4];
int unionCode[4];
int empId=5;
int i;
fstream readfile;//tells fstream what we will call inbound file
readfile.open("employee_data_in.txt", ios::in);//identifies and opens inbound file
ofstream outfile("employee_data_in.txt", ios::out | ios::in);//identifies outbound file
//accepts user input of last name for record search
cout<< "Please enter last name of employee to be deleted" <<endl;
cin>> empName;
for (i=0;i<4;i++){//for loop to search for employee in database
if (empName == empLastName[i]){
empId = i;
break;
}
}
if (empId != 0 && empId != 1 && empId != 2 && empId != 3 ){//result if no record found
cout << "Invalid Entry. ";
}
else{ //result if a record is found
cout << empLastName[empId]<<endl;
cout << payrollType[empId]<<endl;
cout << hoursWorked[empId]<<endl;
cout << payRate[empId]<<endl;
cout << unionCode[empId]<<endl;
}
readfile.close();//closes external file
cout<< "Record deleted ";//display message confirming deletion
}
The directions for the deletion are "don't include the record when you write the .txt file from arrays to the .txt file, but do write the data from arrays to the .txt file."
I tried putting in payrollType[empId]=" ", hoping to replace the spot in the file with dead space, but I ended up with compiling errors of "invalid conversion from const char* to char", and similar with my int variables (hours, rate, and union code).
I also tried reversing my add and modify functions, to no avail (adding blank spaces to the desired locations).
FYI, the external file will already exist in the professor's computer, and no vectors are allowed.
Use a memory mapped file and you don't need to do any file I/O, operating system will do it for you behind the scenes. You will only 'see' a big memory chunk (without the file content be actually in memory).
Is a memory mapped file really suitable for text files? If you change something it might take more or less bytes so it has to change the offset of other data.
How would I read data in then? I thought the use of readfile.open was the read-in. I know my deletion portion should go between lines 38 and 39, right?
I've seen and read that tutorial, but it doesn't explain how to delete items from an external file. I tried just replacing the data to be deleted with blank spaces, but ran into that const* char error, despite me not having and const char variables.
Sorry for the "dumb" questions, it's the end of a semester with a professor who doesn't give lectures or office hours. I was given a textbook and expected to teach myself to program. I've done ok, but what a miserable semester it has been!