Abstract
In order to protect their intellectual property rights, the company will define its own legal format, and its header area will define each byte representing a certain level of information, therefore, you often need to directly import bytes of binary bytes.
Introduction
Environment: Windows XP SP3 + visual c ++ 6.0 SP6
0x33 byte in WF. Bin will be written to 0xac.
Writebyte. C/C
1 /*
2 (C) oomusou 2011 Http://oomusou.cnblogs.com
3
4 Filename: writebyte. c
5 Compiler: Visual C + + 6.0
6 Description: How to Write byte value with N-byte position?
7 Release: oct.31, 2011 1.0
8 */
9
10 # Include <stdio. h>
11
12 Int Main (){
13 File * FP;
14 Int Filesize;
15 Unsigned Char Buff [ 1 ];
16
17 Fp = fopen ( " ./WF. Bin " , " RB + " );
18 If (! FP ){
19 Fclose (FP );
20 Return - 1 ;
21 }
22
23 Buff [ 0 ] = 0xac ;
24
25 Fseek (FP, 0x33 , Seek_set );
26 Fwrite (buff, Sizeof (Unsigned Char ), 1 , FP );
27
28 Fclose (FP );
29
30 Return 0 ;
31 }
17 rows
Fp = fopen ("./WF. Bin","RB +");
Run WF. Bin on the drive. RB indicates read binary, and + indicates that write can be performed in addition to read.
23 rows
Buff [0] =0xac;
Therefore, if you want to merge a byte with the value 0 x AC, first declare the buff char array and enter 0 x AC.
25 rows
Fseek (FP,0x33, Seek_set );
Use fseek to move the location of binary file to 0x33. seek_set indicates that the offset starts from the beginning.
26 rows
Fwrite (buff,Sizeof(UnsignedChar),1, FP );
Use fwrite to merge the buff into the commit case.
Download the complete program
Writebyte.7z
Conclusion
The above Code seems to be very straightforward, but I spent 17 lines a day. I originally thought that since it was intended to merge binary files, so it seems like this:
Fp = fopen ("./WF. Bin","WB");
WB indicates write binary, which seems to be very straightforward, but the result is that the cursor changes to the position 0x33, and the result is actually 0xac,However, this is the end of the case, and the following information is totally lost !!
Why?
In the C in a nutshell coding method and program naming convention [1] p.208, row 7th:
If the mode string starts with "R", this case must exist in the validation system. If the mode string starts with W, a new case will be created if the case does not exist; if so, the content will be lost, in write, the fopen () function reduces the length of the write case to 0.
That is to say, because WB is used to start a file and fseek () is used to move the location of binary file to 0x33, therefore, after 0x33, the latency is reduced to 0, and then fwrite () changes 0 x AC to 0x33.
So it was just a small difference, but the day difference was 10 minutes and eight thousand miles.
Here, I would like to recall the meaning of the combination of fopen () All metric data [2]
R |
Open the text file to allow the cursor to retrieve |
W |
Generate new text file or truncate existing text file to zero length so that |
A |
Attach. Generate a new text file or create a text file so that the file can be written at the end of the file. |
RB |
The binary file can be created to retrieve |
WB |
Generate a new binary file or set the current binary file to zero length so that |
AB |
Attach. Generate a new binary file or create a binary file so that the file can be written at the end of the file. |
R + |
The text file is opened to facilitate updates (updated and updated) |
W + |
Generate a new text file or set the existing text file to zero for update. |
A + |
Attach. Generate a new text file or create a text file to update the file. |
R + B or RB + |
Open a binary file to update (updated and updated) |
W + B or WB + |
Generate new binary file or truncate existing file to zero length for update |
A + B or AB + |
Attach. Generate a new binary file or truncate the existing file to zero length for update. |
See also
(Reporter) How does one import a byte of binary file into the value of N byte? (C/C ++) (c)
Reference
[1] Peter Prinz & Tony Crawford 2005, Cai, C in a nutshell Modeling Method and program naming convention, United States Business Consulting Co., Ltd.
[2] p. j plauger, the standard C library