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系統程式設計 第二版
54
|
第二章
截短檔案
Linux
提供了兩個系統呼叫,可用來截短一個檔案的長度,它們是各項
POSIX
標準(在
不同程度上)所定義和規定的系統呼叫。它們是:
#include <unistd.h>
#include <sys/types.h>
int ftruncate (int fd, off_t len);
與:
#include <unistd.h>
#include <sys/types.h>
int truncate (const char *path, off_t len);
這兩個系統呼叫會把所指定的檔案截短成
len
所指定的值。
ftruncate()
系統呼叫可用來
處理
fd
所指定的檔案描述器,而且該檔案描述器必須開啟以備寫入。
truncate()
系統呼
叫可用來處理
path
所指定的檔案,而且該檔案必須是可寫入的。執行成功時,這兩個呼
叫都會傳回
0
;發生錯誤時,它們都會傳回
-1
並把
errno
設定為適當的值。
這兩個系統呼叫的最常見用法,就是把一個檔案的大小刪節成比當前的長度還短。成功
返回後,檔案的新長度為
len
。先前存在於
len
與舊長度之間的資料會被丟棄,所以再
也無法被讀取要求所存取。
這兩個函式還可用來把一個檔案“截短”成較大的尺寸,這類似於第
2
章〈找尋檔案末
端之後的位置〉一節中所提到之找尋操作與寫入操作的組合。所擴增的位元組會被填入
零值。
這兩項操作都不會更動當前的檔案位置。
舉例來說,假設長度
74
個位元組的檔案
pirate.txt
具有下面的內容:
Edward Teach was ...
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