Thread: Copying every 4th byte

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User
    Join Date
    Jan 2020
    Posts
    12

    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.

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