1

[Please don't comment on using Turbo C++. I know that it's obsolete but we are taught this way only.] Somewhat similar kind of error is here Why do I get a 'ÿ' char after every include that is extracted by my parser? - C but I couldn't relate it to my code - Well I'm a newbie.

#include<fstream.h>
#include<conio.h>
void main()
{
 clrscr();
 char ch;
 ifstream read_file;
 read_file.open("Employee.txt");
 ofstream write_file;
 write_file.open("Another.txt");

 while(!read_file.eof())
 {
 /*Also when I use, write<<read_file.get(ch) in this block instead of the two statements below, it writes some kind of address in the file. Please tell me about that too why it happens. */

  read_file.get(ch); 
  write_file<<ch; 
 }
 read_file.close();
 write_file.close();
 getch();
}

The problem I'm facing is that it appends ÿ character at the end of 'another' file.

eg: the text in "Employee" is, ID:1 Name:abc then the text it copies to "another" is: ID:1 Name:abcÿ

Community
  • 1
  • 1
T.M
  • 817
  • 11
  • 17

2 Answers2

2

The eof() check won't return true once you've read the last character; it stays false until after you've tried to read past the end. So rather than checking for eof in the while-loop condition, check for it right after you read (but before you write), and then break.

(Incidentally, a bit of explanation: ÿ is the ANSI character representation of the value 0xFF, which is to say, -1. This is what get() returns to signal EOF. So if you wanted, instead of checking eof(), you could see whether the char equalled -1.)

Sneftel
  • 40,271
  • 12
  • 71
  • 104
  • Let's consider, the text I stated in the question, i.e. ID and name. While looping, it finds 'c' (the last character in the file) and the loop is executed and the c is copied to the file. After that again it checks for the end_of_file which should return true, making the while condition false and control should not go into the loop. Maybe, I'm misinterpreting things. Please guide me what's wrong with my understanding. – T.M Oct 26 '13 at 08:52
  • I got it here http://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-considered-wrong Thanks for your answers too. – T.M Oct 26 '13 at 09:00
1
while(!read_file.eof())

is always wrong. You need

while (read_file.get(ch))

or

while ((ch = read_file.get()) != EOF)
n. m. could be an AI
  • 112,515
  • 14
  • 128
  • 243
  • Alright, but could you tell me why it writes address if the code in the while block is replaced with that single statement? – T.M Oct 26 '13 at 09:23
  • I don't understand your question. What does write which address? – n. m. could be an AI Oct 26 '13 at 11:58
  • When the two statements in the while block are replaced by single statement, write< – T.M Oct 28 '13 at 12:49
  • Because that's wrong. The correct formula is `while (read.get(ch)) write< – n. m. could be an AI Oct 28 '13 at 13:05
  • Sorry but you did not get my point. I've understood that the condition for while loop must not be the check for eof. But that also gives error at last only. But if, I use the wrong condition (which prints the character ÿ at last) and use ONLY the single statement - read_file.get(ch) then some kind of addresses get written in the file "Another" file. I want to ask why does this write address? – T.M Oct 29 '13 at 17:05
  • Please read what I wrote again, carefully. If that doesn't help, ask a separate question. Do not forget an SSCCE. – n. m. could be an AI Oct 29 '13 at 17:22