惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

D
Docker
Simon Willison's Weblog
Simon Willison's Weblog
H
Help Net Security
F
Fortinet All Blogs
H
Heimdal Security Blog
S
Schneier on Security
L
LangChain Blog
博客园 - Franky
酷 壳 – CoolShell
酷 壳 – CoolShell
NISL@THU
NISL@THU
P
Palo Alto Networks Blog
J
Java Code Geeks
博客园 - 【当耐特】
The Last Watchdog
The Last Watchdog
W
WeLiveSecurity
www.infosecurity-magazine.com
www.infosecurity-magazine.com
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
V
Vulnerabilities – Threatpost
I
InfoQ
Recorded Future
Recorded Future
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
C
CERT Recently Published Vulnerability Notes
T
Tenable Blog
腾讯CDC
C
Check Point Blog
量子位
M
MIT News - Artificial intelligence
GbyAI
GbyAI
罗磊的独立博客
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
B
Blog
小众软件
小众软件
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
C
CXSECURITY Database RSS Feed - CXSecurity.com
Stack Overflow Blog
Stack Overflow Blog
P
Proofpoint News Feed
P
Privacy & Cybersecurity Law Blog
V2EX - 技术
V2EX - 技术
T
Threatpost
Engineering at Meta
Engineering at Meta
Attack and Defense Labs
Attack and Defense Labs
T
Tailwind CSS Blog
S
Securelist
The Cloudflare Blog
博客园 - 叶小钗
L
LINUX DO - 最新话题
T
Troy Hunt's Blog
C
Cyber Attacks, Cyber Crime and Cyber Security
爱范儿
爱范儿

博客园 - 真 OO无双

(轉貼) Bit Twiddling Hacks (SOC) (Verilog) (C) (筆記) 如何移除VirtualBox所遺留下的驅動程式? (SOC) (VirtualBox) (筆記) always block內省略else所代表的電路 (SOC) (Verilog) (原創) 如何在Qsys Subsystem使用Interrupt? (SOC) (Nios II) (Qsys) (原創) Qsys或RTL做修改後,Nios II SBT該如何面對新的硬體? (SOC) (Nios II) (Qsys) (原創) 如何解決目錄改變時,Nios II project無法編譯的問題? (SOC) (Nios II) (DE2-70) (原創) Qsys Generation Tab的Simulation設定的意義 (SOC) (Nios II) (Qsys) (筆記) 如何寫入binary file某個byte的值? (C/C++) (C) (筆記) 如何讀取binary file某個byte連續n byte的值? (C/C++) (C) (筆記) 如何讀取binary file某個byte的值? (C/C++) (C) (原創) Altera Technology Roadshow 2011 Taipei (SOC) (Quartus II) (Nios II) (Qsys) (原創) 如何在安裝SELinux的環境執行Quartus II? (SOC) (Quartus II) (Linux) (RedHat) (筆記) 如何得知Quartus II最新版到底修改了哪些東西? (SOC) (Quartus II) (Nios II) (SOPC Builder) (Qsys) (筆記) Qsys resource整理 (SOC) (Nios II) (Qsys) (原創) 如何安裝Linux版本的Quartus II? (SOC) (Quartus II) (Linux) (RedHat) (VirtualBox) (筆記) 如何在RedHat Linux安裝VirtualBox Guest Additions? (SOC) (Linux) (RedHat) (VirtualBox) (筆記) 常用設定暫存器值的編程技巧 (SOC) (C/C++) (C) (Verilog) (原創) Quartus II安裝新觀念:如何將Quartus II安裝在VirtualBox內? (SOC) (Quartus II) (VirtualBox) (筆記) $dispaly()、$strobe()、$monitor() 、$fwrite()與blocking / nonblocking的關係 (SOC) (Verilog) (Debussy) (Verdi)
(筆記) 如何寫入binary file某個byte連續n byte的值? (C/C++) (C)
真 OO无双 · 2011-10-31 · via 博客园 - 真 OO无双

Abstract
通常公司為了保護其智慧財產權,會自己定義檔案格式,其header區會定義每個byte各代表某項資訊,所以常常需要直接對binary檔的某byte直接進行寫入,且連續寫入幾個byte表示某一數值資訊。

Introduction
使用環境:Windows XP SP3 + Visual C++ 6.0 SP6

將寫入wf.bin的0x33 byte處的連續4 byte值。

 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行

宣告4 byte char array。

17行

fp = fopen("./wf.bin", "rb+");

由於要修改目前開啟的binary f ile,所以使用rb+,詳細請參考(筆記) 如何寫入binary file某個byte的值? (C/C++) (C)

23行

buff[0] = 0xAC;
buff[1] = 0xFF;
buff[2] = 0x1B;
buff[3] = 0xAA;

分別設定每個byte的值。

28行

fseek(fp, 0x33, SEEK_SET);

使用fseek將binary file的檔案位置移到0x33處,其中SEEK_SET表示offset是從檔頭開始。

29行

fwrite(buff, sizeof(unsigned char), 4, fp);

正式使用fwrite將buff寫入檔案,由於單位是unsigned char,所以size為4。

See Also
(筆記) 如何寫入binary file某個byte的值? (C/C++) (C)