+ Post New Thread
Results 1 to 16 of 16
  1. #1
    Advanced Member level 5
    Points: 13,214, Level: 27
    Achievements:
    7 years registered

    Join Date
    Aug 2011
    Posts
    2,618
    Helped
    304 / 304
    Points
    13,214
    Level
    27

    Writing one file into another in C++

    Hello,

    I have a BIN file called "some_file.bin"
    I want to copy the contents of this file to another file named : "some_file_2.bin"
    However, while copying I want to skip every 4th byte.

    For example :

    Contents of "some_file.bin" :
    0xA5 0x3 0x11 0x34 0x33 0x12 0x37 0xDE 0xCE 0x0F 0xEE 0xFF 0x77

    Contents of "some_file_2.bin" - after copying ( 0x34 , 0xDE and 0xFF weren't copied ) :
    0xA5 0x3 0x11 0x33 0x12 0x37 0xCE 0x0F 0xEE 0x77

    Can you please post an example of a C++ code that does that ?

    •   AltAdvertisement

        
       

  2. #2
    Super Moderator
    Points: 30,010, Level: 42
    andre_teprom's Avatar
    Join Date
    Nov 2006
    Location
    Brazil
    Posts
    8,951
    Helped
    1134 / 1134
    Points
    30,010
    Level
    42
    Blog Entries
    9

    Re: Writing one file into another in C++

    Instead of asking for something that does exactly what you want, why don't you look for an example that makes the exact copy, and then seeks into the code for the part that scan each byte and then put there your own logic (cyclically skipping one at each four bytes) ?
    --------------------------------------------------------------------------------------------------
    Part of the world that you live in, You are the part that you're giving ( Renaissance )



  3. #3
    Advanced Member level 5
    Points: 13,214, Level: 27
    Achievements:
    7 years registered

    Join Date
    Aug 2011
    Posts
    2,618
    Helped
    304 / 304
    Points
    13,214
    Level
    27

    Re: Writing one file into another in C++

    That's exactly what I tried to do before posting.
    I found too many examples that supposedly do the same thing in many different ways. Some of them didn't even compile...

    My request is for someone on this forum to post an example that does what I described and for us to dissect the code step-by-step and learn about the nuances (compared to C).



  4. #4
    Super Moderator
    Points: 82,682, Level: 70
    Achievements:
    7 years registered
    Awards:
    2nd Helpful Member
    betwixt's Avatar
    Join Date
    Jul 2009
    Location
    Aberdyfi, West Wales, UK
    Posts
    13,580
    Helped
    4519 / 4519
    Points
    82,682
    Level
    70

    Re: Writing one file into another in C++

    1. open "some_file.bin" in input mode.
    2. create "new_file.bin" in append mode.
    3. initialize a variable as a counter.
    4. read the input file byte
    5. if the counter modulo 4 isn't zero, write the byte to the output file
    6. increment the counter.
    7. stop when you reach the end of the input file.
    8. close both files.

    The actual coding isn't much different to the list of instructions above, you should be able to work it out for yourself.

    Brian.
    PLEASE - no friends requests or private emails, I simply don't have time to reply to them all.
    It's better to share your questions and answers on Edaboard so we can all benefit from each others experiences.


    1 members found this post helpful.

  5. #5
    Super Moderator
    Points: 263,610, Level: 100
    Awards:
    1st Helpful Member

    Join Date
    Jan 2008
    Location
    Bochum, Germany
    Posts
    46,054
    Helped
    14006 / 14006
    Points
    263,610
    Level
    100

    Re: Writing one file into another in C++

    learn about the nuances (compared to C).
    Presumedly the code uses a few basic file-I/O function calls and no C++ specific language elements. No good example to learn about C++.



    •   AltAdvertisement

        
       

  6. #6
    Advanced Member level 5
    Points: 13,214, Level: 27
    Achievements:
    7 years registered

    Join Date
    Aug 2011
    Posts
    2,618
    Helped
    304 / 304
    Points
    13,214
    Level
    27

    Re: Writing one file into another in C++

    betwixt,
    2. create "new_file.bin" in append mode.
    1. What's the C++ function that does that ?
    2. What are the other modes that a file can be opened in ?



  7. #7
    Super Moderator
    Points: 30,010, Level: 42
    andre_teprom's Avatar
    Join Date
    Nov 2006
    Location
    Brazil
    Posts
    8,951
    Helped
    1134 / 1134
    Points
    30,010
    Level
    42
    Blog Entries
    9

    Re: Writing one file into another in C++

    Quote Originally Posted by shaiko View Post
    1. What's the C++ function that does that ?
    2. What are the other modes that a file can be opened in ?
    Review fundamentals of C++ programming, it is not difficult to find any reference.


    I found too many examples that supposedly do the same thing in many different ways
    Please chose some of them and then ask specific questions regarding the issues you cannot solve by yourself.
    --------------------------------------------------------------------------------------------------
    Part of the world that you live in, You are the part that you're giving ( Renaissance )



    •   AltAdvertisement

        
       

  8. #8
    Advanced Member level 5
    Points: 12,779, Level: 27
    Achievements:
    7 years registered

    Join Date
    Apr 2010
    Posts
    1,938
    Helped
    416 / 416
    Points
    12,779
    Level
    27

    Re: Writing one file into another in C++

    if your task is only to skip(delete) every 4th byte and have a new bin file , use a hexeditor.

    if you have to write a program for doing the same then learn programming.



  9. #9
    Advanced Member level 5
    Points: 13,214, Level: 27
    Achievements:
    7 years registered

    Join Date
    Aug 2011
    Posts
    2,618
    Helped
    304 / 304
    Points
    13,214
    Level
    27

    Re: Writing one file into another in C++

    The (real) file I'm working with has 512M Bytes.
    I haven't found a hex editor that can do it efficiently (not manually).

    Although it would be a nice side effect - my immediate goal isn't to "learn programming".
    I need this modified file for a project I'm working on. You see...I'm trying to save the world.
    And with the extra 4th byte my machine won't work properly - everything will be lost.

    Hope your conscience is clear srizbf...



  10. #10
    Advanced Member level 5
    Points: 12,779, Level: 27
    Achievements:
    7 years registered

    Join Date
    Apr 2010
    Posts
    1,938
    Helped
    416 / 416
    Points
    12,779
    Level
    27

    Re: Writing one file into another in C++

    The size of your file not specified earlier . So hex editor is ruled out.

    Quote Originally Posted by shaiko View Post
    ...Hope your conscience is clear srizbf...
    I am wondering how conscience is related to this .



  11. #11
    Super Moderator
    Points: 30,010, Level: 42
    andre_teprom's Avatar
    Join Date
    Nov 2006
    Location
    Brazil
    Posts
    8,951
    Helped
    1134 / 1134
    Points
    30,010
    Level
    42
    Blog Entries
    9

    Re: Writing one file into another in C++

    I did not spent much time to find a possible solution, few minutes actually, searching for tips on the Web, and added the constraint you have defined, exactly as Brian suggested above at post #4.

    Code:
    FILE *fin, *fout;
    
    fin= fopen("some_file.bin");
    fout = fopen("some_file_2.bin");
    
    int skip = 4, c, idx;
    
    for (idx = 1; c = (getc(fin)) != EOF; ++idx)
    {
      if (idx % skip == 0) 
      {
         putc(fout );
      }
    }
    fclose(fin); 
    fclose(fout);
    You could save a lot of time doing the same, note it is somewhat straightfoward, don't even need to add comments to explain how it works; regardless of how familiar one is with the C++ language, it is possible to distinguish the main operations: Open both files, read each byte from one, skip each 4th byte, and record the result into another file byte per byte, just that.

    Once I did not compiled it, you should make experiments by yourself, and report specific issues you are not able to fix, it is more productive than keep posting argumentations not code related.
    --------------------------------------------------------------------------------------------------
    Part of the world that you live in, You are the part that you're giving ( Renaissance )


    1 members found this post helpful.

  12. #12
    Advanced Member level 5
    Points: 13,214, Level: 27
    Achievements:
    7 years registered

    Join Date
    Aug 2011
    Posts
    2,618
    Helped
    304 / 304
    Points
    13,214
    Level
    27

    Re: Writing one file into another in C++

    I tried to append (binary not string) 0x01 to the end of the existing file.
    This is my code:
    Code:
    #include <iostream>
    #include <fstream>
    using namespace std ;
    
    int main ( )
    {
        ofstream some_file ("some_file.bin", ios::out | ios::app | ios::binary);
        some_file << 0x01 ;
        some_file.close( );
        return 0;
    }
    I observed the contents of the file with a HEX editor.
    I didn't see 0x01. Instead I found 0x31.
    What went wrong ?

    - - - Updated - - -

    Thanks andre_teprom!
    I decided to do it with C++ from scratch myself. I'll use your code as basis.
    Can you help with #12 ?
    Last edited by shaiko; 29th December 2019 at 19:28.



  13. #13
    Super Moderator
    Points: 30,010, Level: 42
    andre_teprom's Avatar
    Join Date
    Nov 2006
    Location
    Brazil
    Posts
    8,951
    Helped
    1134 / 1134
    Points
    30,010
    Level
    42
    Blog Entries
    9

    Re: Writing one file into another in C++

    Quote Originally Posted by shaiko View Post
    Can you help with #12 ?
    No, I never used the ofstream method, and have no idea how this works.
    BTW, why not starting with a even simplest approach, such as suggested on #11?
    --------------------------------------------------------------------------------------------------
    Part of the world that you live in, You are the part that you're giving ( Renaissance )



    •   AltAdvertisement

        
       

  14. #14
    Super Moderator
    Points: 82,682, Level: 70
    Achievements:
    7 years registered
    Awards:
    2nd Helpful Member
    betwixt's Avatar
    Join Date
    Jul 2009
    Location
    Aberdyfi, West Wales, UK
    Posts
    13,580
    Helped
    4519 / 4519
    Points
    82,682
    Level
    70

    Re: Writing one file into another in C++

    Andre, your code example is missing the file opening modes but I think most compilers would use 'r' and 'w' by default anyway. It is C rather than C++ but should still work fine. I love C and find C++ overly long and complicated.

    Shaiko, your code in post #12 should work in principle but I suspect you actually created a single byte file with 0x01 in it rather than appending to an existing file. 0x31 is character '1' in ASCII but the program should not have done any conversion.

    Brian.
    PLEASE - no friends requests or private emails, I simply don't have time to reply to them all.
    It's better to share your questions and answers on Edaboard so we can all benefit from each others experiences.


    1 members found this post helpful.

  15. #15
    Advanced Member level 5
    Points: 13,214, Level: 27
    Achievements:
    7 years registered

    Join Date
    Aug 2011
    Posts
    2,618
    Helped
    304 / 304
    Points
    13,214
    Level
    27

    Re: Writing one file into another in C++

    I suspect you actually created a single byte file with 0x01 in it rather than appending
    Why ?
    I explicitly used "ios::app"
    Doesn't this open the file in append mode ?



  16. #16
    Super Moderator
    Points: 263,610, Level: 100
    Awards:
    1st Helpful Member

    Join Date
    Jan 2008
    Location
    Bochum, Germany
    Posts
    46,054
    Helped
    14006 / 14006
    Points
    263,610
    Level
    100

    Re: Writing one file into another in C++

    You misunderstand the operation of fstream << operator. Without format specifier, it does decimal output, hence you see 0x31. For binary byte output, you more simply use ostream::put, or the generic C code suggested in post #11.


    1 members found this post helpful.

--[[ ]]--