Deleting a part of an external file via C++

Hey All,

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:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
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."

Any ideas?
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.
Last edited on
A friend also had me try payrollType[empId]="\0" and I got the same error.
First read in the data from file. Then manipulate the data. Last, write the data back to file. Currently your code don't do any file IO.
Last edited on
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.
Peter- would the data not be considered read in because of lines 12 and 13?

1
2
     fstream readfile;//tells fstream what we will call inbound file
     readfile.open("employee_data_in.txt", ios::in);//identifies and opens inbound file 
That only opens the file to be ready to read from but you are not reading any data.
Modoran, could you explain what you mean by mapped file? This is my first programming class, we didn't even cover vectors, the text was a tad limited.
@Peter

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?
Look over this tutorial and see if it helps with the I/O aspect.
http://cplusplus.com/doc/tutorial/files/
@Zhuge

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!
Bump...
Not solved, as per usual. Just marking it as solved because I'm polite. Goodbye programming!
Topic archived. No new replies allowed.