Thread: Copying every 4th byte

  1. #1
    Registered User
    Join Date
    Jan 2020
    Posts
    2

    Copying every 4th byte

    Hello,

    I want to copy every 4th byte of file_1.bin into file_2.bin
    For example - if the contents of file_1.bin are :

    0x0 // Byte 1
    0x1 // Byte 2
    0x2 // Byte 3
    0xAB // Byte 4 Skip when copying.
    0x3 // Byte 1
    0x4 // Byte 2
    0x5 // Byte 3
    0xAA // Byte 4 Skip when copying.
    0x6 // Byte 1
    0x7 // Byte 2
    0x8 // Byte 3
    0xFF // Byte 4 Skip when copying.
    0x9 // Byte 1
    0xA // Byte 2
    0xB // Byte 3
    0x1 // Byte 4 Skip when copying.
    0xC // Byte 1

    the contents of file_2.bin will be :

    0x0
    0x1
    0x2
    0x3
    0x4
    0x5
    0x6
    0x7
    0x8
    0x9
    0xA
    0xB
    0xC

    file_1.bin can be any size.
    This is the C++ program that I wrote - but it doesn't work.
    Please help me fix it :

    Code:
    #include <iostream>
    #include <fstream>
    using namespace std ;
    
    
    int main ( )
    {
        ifstream file_1 ( "file_1.bin", ios::out | ios::app | ios::binary ) ;
        ofstream file_2 ( "file_2.bin", ios::out | ios::app | ios::binary ) ;
        int index ;
        char ch ;
        for ( index = 1 ; file_1.eof ( ) != 1 ; index ++ )
        {
            if ( index % 4 )
            {
                file_1.get(ch) ;
                file_2 << ch ;
            }
        }
        return 0 ;
    }
    Last edited by bitslip; 01-07-2020 at 06:00 PM.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    27,561
    I'm a bit (byte?) confused: do you want to copy every 4th byte or do you want to copy all the bytes except every 4th byte?

    Either way, your for loop is wrong as you're not always reading a byte from the input file. I would suggest:
    Code:
    for ( int index = 0; file_1.get(ch); ++index )
    {
        if ( index % 4 == 3 )
        {
            file_2 << ch ;
        }
    }
    The above would copy every 4th byte. If you want to copy all the bytes except every 4th byte, then you just invert the if statement condition.
    Last edited by laserlight; 01-07-2020 at 06:17 PM.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Jan 2020
    Posts
    2
    Thanks!
    It worked.

    Can you please point out what where the problem with how I wrote the code ?
    Last edited by bitslip; 01-08-2020 at 03:50 AM.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    27,561
    As I mentioned, the problem is that you're not always reading a byte on each iteration of the loop. That means that when you want to skip a byte, you don't: it just ends up read on the next iteration instead.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. byte array copying problem
    By TmX in forum C Programming
    Replies: 2
    Last Post: 08-12-2016, 11:44 PM
  2. Replies: 5
    Last Post: 01-22-2016, 03:20 AM
  3. Why is there one extra byte when copying file?
    By xbfish in forum C Programming
    Replies: 3
    Last Post: 07-21-2011, 03:15 PM

Tags for this Thread