# Writing one file into another in C++

#### shaiko

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 ?

#### andre_teprom

##### Super Moderator
Staff member
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) ?

#### shaiko

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).

#### betwixt

##### Super Moderator
Staff member
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.

shaiko

### shaiko

points: 2

#### FvM

##### Super Moderator
Staff member
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++.

#### shaiko

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 ?

#### andre_teprom

##### Super Moderator
Staff member
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.

#### srizbf

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.

#### shaiko

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...

#### srizbf

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

...Hope your conscience is clear srizbf...
I am wondering how conscience is related to this . :-( :|

#### andre_teprom

##### Super Moderator
Staff member
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.

shaiko

### shaiko

points: 2

#### shaiko

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:

#### andre_teprom

##### Super Moderator
Staff member
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?

#### betwixt

##### Super Moderator
Staff member
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.

shaiko

### shaiko

points: 2

#### shaiko

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 ?

#### FvM

##### Super Moderator
Staff member
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:ut, or the generic C code suggested in post #11.

shaiko

points: 2