I am writing a program to read/write from a text file which contain employee information. Each employee information is stored inside a textfile like below. Employee information is stored to four lines.
W00051 M
Christopher Tan
1200.00 150.00 1400.20 156.00 200.00 880.00 1500.00 8000.00 800.00 120.00 1600.00 1800.00
1280.00 1500.00 140.80 1523.00 2000.00 2300.00 2600.00 8800.00 19800.00 1221.00 3000.00 1900.00
W00012 E
Janet Lee
2570.00 2700.00 3000.00 3400.00 4000.00 13000.00 200.00 450.00 1200.00 8000.00 4500.00 9000.00
1238.00 560.00 6700.00 1200.00 450.00 789.00 67.90 999.00 3456.00 234.00 900.00 2380.00
I have a delete employee function which accept employee id (W00012) and delete the row which contains the employee information.The updated file is stored inside tempfilesource.
void delete_employee(char filesource[],char tempfilesource[],int employee_line,char inputid[])
{
char charline[255];
string line;
int linecount = 1;
ifstream inputempfile;
ofstream outputempfile;
inputempfile.open(filesource);
outputempfile.open(tempfilesource);
outputempfile.precision(2);
outputempfile.setf(ios::fixed);
outputempfile.setf(ios::showpoint);
if (inputempfile.is_open())
{
while (getline(inputempfile,line))
{
if((linecount<employee_line || linecount>employee_line+3))
{
outputempfile<< line;
}
linecount++;
}
inputempfile.close();
outputempfile.close();
}
}
The problem occurs when the employee I want to delete is located on the bottom of the textfile. And the updated file contains a blank newline:
W00051 M
Christopher Tan
1200.00 150.00 1400.20 156.00 200.00 880.00 1500.00 8000.00 800.00 120.00 1600.00 1800.00
1280.00 1500.00 140.80 1523.00 2000.00 2300.00 2600.00 8800.00 19800.00 1221.00 3000.00 1900.00
<blank newline>
How can I prevent a newline from being written to the file?