當前位置:首頁 » 編程語言 » fread函數c語言

fread函數c語言

發布時間: 2025-02-19 22:14:29

Ⅰ 關於C語言fread的用法

簡介
fread


能:
從一個流中讀數據

函數原型:
size_t
fread(
void
*buffer,
size_t
size,
size_t
count,
file
*stream
);


數:

1.用於接收數據的地址(指針)(buffer)

2.單個元素的大小(size)
:單位是位元組而不是位,例如讀取一個整型數就是2個位元組

3.元素個數(count)

4.提供數據的文件指針(stream)

返回值:成功讀取的元素個數
程序例
#include

int
main(void)

{

file
*stream;

char
msg[]
=
"this
is
a
test";

char
buf[20];

if
((stream
=
fopen("mmy.fil",
"w+"))
==
null)

{

fprintf(stderr,
"cannot
open
output
file.\n");

return
1;

}
/*
write
some
data
to
the
file
*/

fwrite(msg,
strlen(msg)+1,
1,
stream);
/*
seek
to
the
beginning
of
the
file
*/

fseek(stream,
0,
seek_set);
/*
read
the
data
and
display
it
*/

fread(buf,
strlen(msg)+1,
1,stream);

printf("%s\n",
buf);

fclose(stream);

return
0;

}

熱點內容
java編譯器在哪裡執行 發布:2025-05-04 13:42:44 瀏覽:373
我的世界海島生存伺服器 發布:2025-05-04 13:35:22 瀏覽:864
pythonsd 發布:2025-05-04 13:29:02 瀏覽:564
pythonlogging格式 發布:2025-05-04 13:18:58 瀏覽:851
起亞k3壓縮比是多少 發布:2025-05-04 13:16:56 瀏覽:539
java字元串切割 發布:2025-05-04 13:16:46 瀏覽:987
物流管理存儲 發布:2025-05-04 13:16:45 瀏覽:722
18款昂科拉哪個配置最值得購買 發布:2025-05-04 13:13:49 瀏覽:481
unbuntu18編譯vlc 發布:2025-05-04 12:52:19 瀏覽:487
ecshop手機源碼 發布:2025-05-04 12:52:13 瀏覽:200