當前位置:首頁 » 編程語言 » 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;

}

熱點內容
電視下架緩存的還能看嗎 發布:2025-07-12 19:14:12 瀏覽:444
安卓平板微軟平板和蘋果哪個好 發布:2025-07-12 19:09:37 瀏覽:413
資料庫地區 發布:2025-07-12 19:05:41 瀏覽:395
如何檢查vds腳本 發布:2025-07-12 19:04:24 瀏覽:908
命令行編譯vs2013 發布:2025-07-12 19:01:22 瀏覽:808
c語言輸出所有素數 發布:2025-07-12 19:01:19 瀏覽:659
查電費賬號密碼多少 發布:2025-07-12 18:56:19 瀏覽:545
吉利遠景x1標配配置有哪些 發布:2025-07-12 18:56:18 瀏覽:533
智能演算法pdf 發布:2025-07-12 18:46:07 瀏覽:400
php生成短鏈接 發布:2025-07-12 18:44:37 瀏覽:843