Skip to Content
For Enterprise
For Government
For Higher Ed
For Individuals
For Marketing
For Enterprise
For Government
For Higher Ed
For Individuals
For Marketing
Explore Skills
Cloud Computing
Microsoft Azure
Amazon Web Services (AWS)
Google Cloud
Cloud Migration
Cloud Deployment
Cloud Platforms
Data Engineering
Data Warehouse
SQL
Apache Spark
Microsoft SQL Server
MySQL
Kafka
Data Lake
Streaming & Messaging
NoSQL Databases
Relational Databases
Data Science
Pandas
R
MATLAB
SAS
D3
Power BI
Tableau
Statistics
Exploratory Data Analysis
Data Visualization
AI & ML
Generative AI
Machine Learning
Artificial Intelligence (AI)
Deep Learning
Reinforcement Learning
Natural Language Processing
TensorFlow
Scikit-Learn
Hyperparameter Tuning
MLOps
Programming Languages
Java
JavaScript
Spring
Python
Go
C#
C++
C
Swift
Rust
Functional Programming
Software Architecture
Object-Oriented
Distributed Systems
Domain-Driven Design
Architectural Patterns
IT/Ops
Kubernetes
Docker
GitHub
Terraform
Continuous Delivery
Continuous Integration
Database Administration
Computer Networking
Operating Systems
IT Certifications
Security
Network Security
Application Security
Incident Response
Zero Trust Model
Disaster Recovery
Penetration Testing / Ethical Hacking
Governance
Malware
Security Architecture
Security Engineering
Security Certifications
Design
Web Design
Graphic Design
Interaction Design
Film & Video
User Experience (UX)
Design Process
Design Tools
Business
Agile
Project Management
Product Management
Marketing
Human Resources
Finance
Team Management
Business Strategy
Digital Transformation
Organizational Leadership
Soft Skills
Professional Communication
Emotional Intelligence
Presentation Skills
Innovation
Critical Thinking
Public Speaking
Collaboration
Personal Productivity
Confidence / Motivation
Features
All features
Verifiable skills
AI Academy
Courses
Certifications
Interactive learning
Live events
Superstreams
Answers
Insights reporting
Radar Blog
Buy Courses
Plans
Sign In
Try Now
O'Reilly Platform
book
LINUX系統程式設計 第二版
by
Robert Love
December 2013
Intermediate to advanced
496 pages
8h 57m
Chinese
GoTop Information, Inc.
Content preview from
LINUX系統程式設計 第二版
緩衝式
I/O
|
83
讀取二元資料
對於某些應用程式而言,光是讀取個別的字符或輸入列是不夠的。有時候,開發者會想
要讀寫複雜的二元的資料,例如
C
的結構。為此,標準
I/O
程式庫提供了
fread()
:
#include <stdio.h>
size_t fread (void *buf, size_t size, size_t nr, FILE *stream);
fread()
可從
stream
把資料讀進
buf
所指向的緩衝區,所讀取的位元組數目由
size*nr
來
決定(資料中共有
nr
個元素,每個元素具有
size
個位元組)。檔案指標會前進到實際所
讀取到位元組數目。
執行成功時,
fread()
會傳回所讀取到的元素數目(而不是位元組數目!)。執行失敗
時,它會傳回一個小於
nr
的值,指出發生了錯誤或是讀取到了
EOF
(檔案末端)。可
惜,若不使用
ferror()
和
feof()
(見第
3
章〈錯誤與
EOF
〉一節),則無法知道所發生
的是這兩種情況的哪一種。
因為變數大小(
variable size
)、對齊(
alignment
)、補白(
padding
)以及位元組順序
(
byte oeder
)的不同,一個應用程式所寫入的二元資料未必可以被不同的應用程式所讀
取,或者未必可以被不同機器上的相同應用程式所讀取。
對齊的問題
所有機器架構都會有
資料對齊
(
data alignment
)的需求。程式設計者往往會把記
憶體想成是一個由位元組所構成的陣列。然而,我們的處理器對記憶體進行讀寫
操作時,並不會使用位元組大小的團塊(
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial
You might also like
優雅的SciPy|Python科學研究的美學
Juan Nunez-Iglesias, Stéfan van der Walt, Harriet Dashnow
成為卓越程式設計師的38項必修法則
Pete Goodliffe
深入理解運算原理|從簡單的機器到無所不能的程式
Tom Stuart
C++语言导学(原书第2版)
本贾尼 斯特劳斯特鲁普
Publisher Resources
ISBN: 9789862769812