従業員情報を含むテキスト ファイルを読み書きするプログラムを作成しています。各従業員情報は、以下のようなテキストファイル内に保存されます。従業員情報は 4 行に格納されます。
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
従業員ID(W00012)を受け入れ、従業員情報を含む行を削除する従業員削除機能があります。更新されたファイルは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();
}
}
削除したい従業員がテキストファイルの一番下にある場合に問題が発生します。また、更新されたファイルには空白の改行が含まれています。
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>
改行がファイルに書き込まれないようにするにはどうすればよいですか?