0

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?

Axel
  • 1,053
  • 9
  • 19
  • How are you checking for the new line? Text files generally do have an extra `\n` at the end of the file. However, it's usually hidden by your text editor. Does the new line show up in the text editor? – Joseph Mansfield Apr 10 '13 at 10:05

3 Answers3

0

There are at least two options:

  • You can check whether the written line is the last line and trim the string before writing it.

  • You can delete the last character from the file after you have done writing.

Dariusz
  • 21,561
  • 9
  • 74
  • 114
  • Yes, the newline shows on my text editor, I am using sublime text. The original text file does not have a blank new line. – Axel Apr 10 '13 at 10:08
0

Do not use eof() as your condition when extracting from files. It does not give a good indication of whether there is actually anything left to extract. Change it to:

while (getline(inputempfile,line))
{
  if((linecount<employee_line || linecount>employee_line+3))
  {
    outputempfile<< line;
  }
  linecount++;
}

Text files typically end with an extra \n which is hidden by text files. As you had it, when you were iterating through, the last line would be read and that final \n would be extracted. Since getline doesn't care to read beyond the \n (that is the delimiter after all), it doesn't see that it's reached the end, so the EOF bit isn't set. That means the next iteration continues even though there is nothing left to read, getline extracts the nothingness at the end of the file and then you write it to the output. This gives you this extra line.

Joseph Mansfield
  • 108,238
  • 20
  • 242
  • 324
  • Thanks for the remind. I have changed it and there is still new blank newline. – Axel Apr 10 '13 at 10:12
  • @soulchild Hm... how are you determining that there is a blank line? In a text editor? Actually, I don't understand. You shouldn't be getting any newlines. You aren't doing `<< endl`. Are you sure this is your real code? – Joseph Mansfield Apr 10 '13 at 10:13
  • my text editor shows line count on the left side.After I delete the employee which is located at bottom, 4 lines of data was shown but my text editor shows the line count '5'. Yep, this is my real code. – Axel Apr 10 '13 at 10:19
  • @soulchild Well I'm baffled then. Why are you getting any newlines at all? – Joseph Mansfield Apr 10 '13 at 10:23
  • `getline` reads a text from the input file, up to and including the newline character. The fourth line of the input file ends with a newline, which you read and print. – riv Apr 10 '13 at 10:26
  • @riv It extracts the newline but it does not add it to `line`. [See this example](http://ideone.com/YWf1BB). – Joseph Mansfield Apr 10 '13 at 10:26
  • @soulchild Just a thought... did you create the text file on Windows and now you're running this code on Linux? – Joseph Mansfield Apr 10 '13 at 10:27
0

alternatively u dont write "line" variable to "outputempfile" if line variable contains only "\n"

ie something like this:

while (getline(inputempfile,line))
{
  if((linecount<employee_line || linecount>employee_line+3) && strcmp(line,"\n")==0)
  {
    outputempfile<< line;
  }
  linecount++;
}

not sure about the syntax but the idea should work

phenom
  • 3
  • 1