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系統程式設計 第二版
86
|
第三章
fwrite()
可將引數
buf
所指向的資料寫入引數
stream
所指向的串流,所寫入的位元組數
目由
size*nr
來決定(資料中共有
nr
個元素,每個元素具有
size
個位元組)。檔案指標
會前進到實際所寫入的位元組數目。
執行成功時,
fwrite()
會傳回所寫入的元素數目(而不是位元組數目!)。執行失敗
時,則會透過傳回一個小於
nr
的值,指出發生了錯誤。
使用緩衝式 I/O 的簡單程式
現在讓我們來看一個例子—事實上,這是一個完整的程式—它整合進了本章迄今為止所
介紹的許多介面。此程式首先會定義
struct pirate
,接著以該型別宣告兩個變數。此
程式會初始設定其中一個變數,隨後把它寫入磁碟(也就是,經由輸出串流寫入檔案
data
)。然後此程式會經由不同的串流從
data
檔案把資料直接讀回
struct pirate
的另一
個實例(instance)。最後,此程式會把該結構的內容寫入標準輸出:
#include <stdio.h>
int main (void)
{
FILE *in, *out;
struct pirate {
char name[100]; /* 真實姓名 */
unsigned long booty; /* 以英鎊為單位 */
unsigned int beard_len; /* 以英吋為單位 */
} p, blackbeard = { "Edward Teach", 950, 48 };
out = fopen ("data", "w");
if (!out) { ...
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