Click here to Skip to main content
15,892,005 members
Please Sign up or sign in to vote.
3.40/5 (2 votes)
See more:
C++
#include <iostream.h>
#include <fstream.h>
#include <conio.h>
void main()
{
    clrscr();
    char ch;
    ifstream infile;
    ofstream outfile;
    infile.open("edf.txt");

    outfile.open("ew.txt");

    while(!infile.eof())
    {
        infile.get(ch);
        outfile.put(ch);
    }

    infile.close();
    outfile.close();
}

The above code reads data from edf.txt file and copy it into ew.txt file
edf.txt                             ew.txt


aliahmed                             aliahmed
ffff                                  ffffÿ

my question is that after copy extra character ÿ is append what is this character and how i remove it
Posted
Updated 13-Jun-13 1:27am
v2

You should also test the ifstream immediately after reading the character, namely
C++
//..
infile.get(ch);
if ( infile.good() ) outfile.put(ch);
// ..


See the sample code[^] at cplusplus.com.
 
Share this answer
 
v2
The previous solution, although entirely correct, does not explain why that second test is necessary. Here is the full story:

After you have read the last character of the stream by
C++
infile.get (ch);

the stream is not yet in the end-of-file state. So the next loop iteration will still occur. Only after get attempts to read beyond the stream end the eof flag in the stream will be set. In that last iteration, in which get hits the end-of-file, ch will be set to the value EOF instead a valid character. And that EOF value (usually a -1) is what you see in your output.

In fact you could have written your loop with only one test like this:
C++
while (true)
{
    infile.get (ch);
    if (!infile.good())
        break;
    outfile.put (ch);
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900