Skip to Content
LINUX系統程式設計 第二版
book

LINUX系統程式設計 第二版

by Robert Love
December 2013
Intermediate to advanced
496 pages
8h 57m
Chinese
GoTop Information, Inc.
Content preview from LINUX系統程式設計 第二版
檔案 I/O
|
51
也可以把
fd
的檔案位置設定到檔案的末端:
off_t ret;
ret = lseek (fd, 0, SEEK_END);
if (ret == (off_t)
1)
/* 錯誤 */
因為
lseek()
會傳回更動後的檔案位置,所以可經由把
pos
設為零的方式,用它來找到
當前的檔案位置:
int pos;
pos = lseek (fd, 0, SEEK_CUR);
if (pos == (off_t)
1)
/* 錯誤 */
else
/* 'pos' fd 的當前位置 */
到目前為止,
lseek()
最常見的用法就是找到檔案的開頭、找到檔案的末端,或是決定
一個檔案描述器的當前檔案位置。
找尋檔案末端之後的位置
你可以要求
lseek()
把檔案指標推進到檔案末端之後的位置。例如,下面的程式碼會把
指標從(
fd
所映射之)檔案的末端推進 1688 個位元組:
int ret;
ret = lseek (fd, (off_t) 1688, SEEK_END);
if (ret == (off_t)
1)
/* 錯誤 */
就其本身而言,將檔案指標推進到檔案末端之後的位置,事實上什麼也沒做—對剛建立
的檔案位置進行讀取操作將傳回 EOF然而,如果隨後對此位置進行寫入操作,則檔案
的舊長度與新長度之間將會建立新的空間,而且會以零來填補它。
這個用零填補的空間稱為
空洞
hole
)。 Unix 風格的檔案系統中,空洞並不佔任何實
體的磁碟空間。這意味著,檔案系統上所有檔案的大小加總起來可能會超過磁碟的實體 ...
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科學研究的美學

優雅的SciPy|Python科學研究的美學

Juan Nunez-Iglesias, Stéfan van der Walt, Harriet Dashnow
C++语言导学(原书第2版)

C++语言导学(原书第2版)

本贾尼 斯特劳斯特鲁普

Publisher Resources

ISBN: 9789862769812