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
|
87
in = fopen ("data", "r");
if (!in) {
perror ("fopen");
return 1;
}
if (!fread (&p, sizeof (struct pirate), 1, in)) {
perror ("fread");
return 1;
}
if (fclose (in)) {
perror ("fclose");
return 1;
}
printf ("name=\"%s\" booty=%lu beard_len=%u\n",
p.name, p.booty, p.beard_len);
return 0;
}
當然,其所輸出的是一開始所設定的值:
name="Edward Teach" booty=950 beard_len=48
此外,請牢記,因為變數大小、對齊…等等的不同,一個應用程式所寫入的二元資料,
未必可以被其他應用程式所讀取。也就是,不同的應用程式—或甚至是不同機器上的
相同應用程式—未必能夠正確讀回
fwrite()
所寫入的資料。我們的例子中,應該考慮
unsigned long
的大小是否會有所不同,或者補白的數量是否會有種種變化。只要你是在
使用特定 ABI 的特定機器類型上執行應用程式,這些事情保證都會維持不變。
查找一個串流
操作當前的串流位置往往會很有用。或許是應用程式正在讀取一個以紀錄為基礎的複雜
檔案,需要來回跳躍。亦或許是串流需要被重新設成位置零。無論是何種情況,標準 I/
O 程式庫提供了一系列「功能相當於 ...
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