Thread: Unwanted char/byte when joining files

  1. #1
    Registered User
    Join Date
    Jun 2009
    Posts
    11

    Unwanted char/byte when joining files

    Hello everyone.

    I wrote the a piece of code to join three files but there is an unwanted char/byte "ÿ" that shouldn't exist but does.

    The result is something like

    Code:
    *FILE1**FILE2*ÿ*FILE3*ÿ
    
    Instead of
    
    *FILE1**FILE2**FILE3*
    Any help would be appriecated.

    My two questions are: Why is it there, and how can I stop it from appearing.

    Code:
    	ifstream in("Text1.txt", ios::in | ios::binary);
    	ofstream out("Text3.txt", ios::out | ios::app | ios::binary);
    
    	if(in.is_open() && out.is_open())
    	{
    		while(!in.eof())
    		{
    			out.put(in.get());
    		}
    	}
    	in.close();
    	out.close();
        
    	ifstream in2("Text2.txt", ios::in | ios::binary);
    	ofstream out2("Text3.txt", ios::out | ios::app | ios::binary);
    
    	if(in2.is_open() && out2.is_open())
    	{
    		while(!in2.eof())
    		{
    			out2.put(in2.get());
    		}
    	}
    	in2.close();
    	out2.close();

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    The two loops of the form
    Code:
    while (!in.eof())
    {
        out.put(in.get());
    }
    are the culprits. If in.get() return a value indicating end of file, that value will be output before the loop ends. The solution is not to loop looking for end of file, and to test the value returned by "in.get()" BEFORE outputting it.


    There is a FAQ here explaining why the equivalent if C (involving a "while (!feof(file))" loop where the body reads from the file) is a bad idea if you want more information.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  3. #3
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Essentially, the problem is that eof() only becomes true AFTER you've made a read that failed.
    So, assume there is one byte left in the file. eof() returns false, get() gives your char and everything is fine.
    Next iteration, eof() still returns false, get() fails and you output garbage to your file. It is for now that eof() signals true.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  4. #4
    Registered User
    Join Date
    Jun 2009
    Posts
    11
    Quote Originally Posted by grumpy View Post
    The two loops of the form
    Code:
    while (!in.eof())
    {
        out.put(in.get());
    }
    are the culprits. If in.get() return a value indicating end of file, that value will be output before the loop ends. The solution is not to loop looking for end of file, and to test the value returned by "in.get()" BEFORE outputting it.


    There is a FAQ here explaining why the equivalent if C (involving a "while (!feof(file))" loop where the body reads from the file) is a bad idea if you want more information.
    Thanks Grumpy.

    I'm a novice who is amazed I even put the two loops together at all. How do I test get? I tried a whole bunch of if statements and stuff but nothing worked. Also, would you mind sharing a link to the FAQ you mentioned?

  5. #5
    Registered User
    Join Date
    Jun 2009
    Posts
    11
    Quote Originally Posted by Elysia View Post
    Essentially, the problem is that eof() only becomes true AFTER you've made a read that failed.
    So, assume there is one byte left in the file. eof() returns false, get() gives your char and everything is fine.
    Next iteration, eof() still returns false, get() fails and you output garbage to your file. It is for now that eof() signals true.
    Thanks Elysia.

    I kinda get what you mean but not enough to know how to correct it. What should I do?

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    You basically test if the read succeeded:

    Code:
    char c;
    while (in.get(c))
        out.put(c);
    If in.get(c) fails (i.e. you're reading beyond the end of the file), then the stream will be put into an error state. You can check if a stream is good or not by simply checking it as if it were a boolean, which is what the loop does.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  7. #7
    Registered User
    Join Date
    Jun 2009
    Posts
    11
    Quote Originally Posted by Elysia View Post
    You basically test if the read succeeded:

    Code:
    char c;
    while (in.get(c))
        out.put(c);
    If in.get(c) fails (i.e. you're reading beyond the end of the file), then the stream will be put into an error state. You can check if a stream is good or not by simply checking it as if it were a boolean, which is what the loop does.
    Thanks a lot Elysia.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. reading files byte by byte
    By cpsc in forum C++ Programming
    Replies: 12
    Last Post: 01-07-2011, 03:54 PM
  2. 1 byte int, not using char
    By since in forum C++ Programming
    Replies: 30
    Last Post: 07-28-2010, 10:16 PM
  3. byte/char
    By l2u in forum C++ Programming
    Replies: 6
    Last Post: 01-10-2007, 05:48 PM
  4. Coverting char to BYTE.
    By Queatrix in forum Windows Programming
    Replies: 7
    Last Post: 11-04-2005, 09:13 PM
  5. Word to byte (or INT to CHAR)
    By Andy_P in forum C Programming
    Replies: 3
    Last Post: 09-30-2005, 07:55 AM