(Reporter) How does one import a byte of binary file into the value of N byte? (C/C ++) (c)

Source: Internet
Author: User

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, it is often necessary to directly import a byte of binary classification, and then directly Import several bytes to indicate a certain amount of data.

Introduction
Environment: Windows XP SP3 + visual c ++ 6.0 SP6

Add the cursor to the 0x33 byte value of WF. Bin.

 1   /*   
2 (C) oomusou 2011 Http://oomusou.cnblogs.com
3
4 Filename: writenbyte. c
5 Compiler: Visual C + + 6.0
6 Description: How to Write n 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 [ 4 ];
16
17 Fp = fopen ( " ./WF. Bin " , " RB + " );
18 If (! FP ){
19 Fclose (FP );
20 Return - 1 ;
21 }
22
23 Buff [ 0 ] = 0xac ;
24 Buff [1 ] = 0xff ;
25 Buff [ 2 ] = 0x1b ;
26 Buff [ 3 ] = 0xaa ;
27
28 Fseek (FP, 0x33 , Seek_set );
29 Fwrite (buff,Sizeof (Unsigned Char ), 4 , FP );
30
31 Fclose (FP );
32
33 Return 0 ;
34 }

15 rows

 
UnsignedCharBuff [4];

4 byte char array.

17 rows

Fp = fopen ("./WF. Bin","RB +");

Because we want to modify the currently enabled binary F ile, how can we import a byte value of binary file using RB + or Alibaba Cloud? (C/C ++) (c ).

23 rows

 
Buff [0] =0xac;
Buff [1] =0xff;
Buff [2] =0x1b;
Buff [3] =0xaa;

Set the value of each byte separately.

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

29 rows

 
Fwrite (buff,Sizeof(UnsignedChar),4, FP );

The official use of fwrite will put the buff into the limit case, because the single bit is unsigned char, so the size is 4.

See also
(Reporter) How to import a byte value of binary file? (C/C ++) (c)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.