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

推荐订阅源

T
The Blog of Author Tim Ferriss
S
Schneier on Security
博客园 - 聂微东
爱范儿
爱范儿
大猫的无限游戏
大猫的无限游戏
有赞技术团队
有赞技术团队
腾讯CDC
博客园 - 叶小钗
WordPress大学
WordPress大学
博客园_首页
J
Java Code Geeks
Last Week in AI
Last Week in AI
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
V
V2EX
Microsoft Azure Blog
Microsoft Azure Blog
The GitHub Blog
The GitHub Blog
N
Netflix TechBlog - Medium
Y
Y Combinator Blog
Schneier on Security
Schneier on Security
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
Recorded Future
Recorded Future
The Register - Security
The Register - Security
C
Cybersecurity and Infrastructure Security Agency CISA
P
Privacy & Cybersecurity Law Blog
P
Proofpoint News Feed
P
Privacy International News Feed
K
Kaspersky official blog
C
CERT Recently Published Vulnerability Notes
阮一峰的网络日志
阮一峰的网络日志
F
Full Disclosure
NISL@THU
NISL@THU
AWS News Blog
AWS News Blog
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
U
Unit 42
MongoDB | Blog
MongoDB | Blog
A
Arctic Wolf
云风的 BLOG
云风的 BLOG
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
D
Darknet – Hacking Tools, Hacker News & Cyber Security
T
Threatpost
D
Docker
人人都是产品经理
人人都是产品经理
T
Tailwind CSS Blog
V2EX - 技术
V2EX - 技术
G
GRAHAM CLULEY
M
MIT News - Artificial intelligence
H
Heimdal Security Blog
N
News and Events Feed by Topic
P
Proofpoint News Feed

博客园 - 真 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連續n byte的值? (C/C++) (C) (筆記) 如何寫入binary file某個byte的值? (C/C++) (C) (筆記) 如何讀取binary file某個byte連續n 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的值? (C/C++) (C)
真 OO无双 · 2011-10-27 · via 博客园 - 真 OO无双

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

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

將讀取wf.bin的0x13 byte處的值。

Method 1:使用fsetpos()

fsetpos.c / C

 1 /* 
2 (C) OOMusou 2011 http://oomusou.cnblogs.com
3
4 Filename : fsetpos.c
5 Compiler : Visual C++ 6.0
6 Description : how to get byte value with n-byte position?
7 Release : oct.27,2011 1.0
8 */
9
10 #include <stdio.h>
11
12 int main() {
13 FILE *fp;
14 fpos_t pos;
15 unsigned char c;
16
17 fp = fopen("./wf.bin", "rb");
18 if (!fp)
19 return 1;
20
21 pos = 0x13;
22 fsetpos(fp, &pos);
23
24 c = fgetc(fp);
25
26 printf("%x\n", c);
27
28 return 0;
29 }

21行

pos = 0x13;
fsetpos(fp, &pos);

直接使用fsetpos()將位置移動到0x13 byte處。

Method 2:使用fseek()

fseek.c / C

 1 /* 
2 (C) OOMusou 2011 http://oomusou.cnblogs.com
3
4 Filename : fseek.c
5 Compiler : Visual C++ 6.0
6 Description : how to get byte value with n-byte position?
7 Release : oct.27,2011 1.0
8 */
9 #include <stdio.h>
10
11 int main() {
12 FILE *fp;
13 unsigned char c;
14
15 fp = fopen("./wf.bin", "rb");
16 if (!fp)
17 return 1;
18
19 fseek(fp, 0x13, SEEK_SET);
20
21 c = fgetc(fp);
22
23 printf("%x\n", c);
24
25 return 0;
26 }

19行

fseek(fp, 0x13, SEEK_SET);

使用fseek()將位置移動到0x13 byte處,SEEK_SET表示從檔案開始處開始offset。

完整程式碼下載
fsetpos.7z (使用fsetpos())
fseek.7z (使用fseek())

See Also
(筆記) 如何讀取binary file某個byte連續n byte的值? (C/C++) (C)