日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

fwrite函数的一般调用形式是什么?

發布時間:2023/12/2 编程问答 37 豆豆
生活随笔 收集整理的這篇文章主要介紹了 fwrite函数的一般调用形式是什么? 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

fwrite() 是C 語言標準庫中的一個文件處理函數,功能是向指定的文件中寫入若干數據塊,如成功執行則返回實際寫入的數據塊數目。該函數以二進制形式對文件進行操作,不局限于文本文件。

語法:

fwrite(buffer,size,count,fp)

參數:

buffer是準備輸出的數據塊的起始地址

size是每個數據塊的字節數

count用來指定每寫一次或輸出的數據塊

fp為文件指針。

函數返回寫入數據的個數。

注意

(1)寫操作fwrite()后必須關閉流fclose()。

(2)不關閉流的情況下,每次讀或寫數據后,文件指針都會指向下一個待寫或者讀數據位置的指針。

讀寫常用類型

(1)寫int數據到文件

#include?

#include?

int main ()

{

??FILE * pFile;

??int buffer[] = {1, 2, 3, 4};

??if((pFile = fopen ("myfile.txt", "wb"))==NULL)

??{

??????printf("cant open the file");

??????exit(0);

??}

??//可以寫多個連續的數據(這里一次寫4個)

??fwrite (buffer , sizeof(int), 4, pFile);

??fclose (pFile);

??return 0;

}

(2)讀取int數據

#include?

#include?

?

int main () {

????FILE * fp;

????int buffer[4];

????if((fp=fopen("myfile.txt","rb"))==NULL)

????{

??????printf("cant open the file");

??????exit(0);

????}

????if(fread(buffer,sizeof(int),4,fp)!=4)?? //可以一次讀取

????{

????????printf("file read error\n");

????????exit(0);

????}

?

????for(int i=0;i<4;i )

????????printf("%d\n",buffer[i]);

????return 0;

}

執行結果:

5.讀寫結構體數據

(1)寫結構體數據到文件

#include?

#include?

#include?

typedef struct{

????int age;

????char name[30];

}people;

?

int main ()

{

????FILE * pFile;

????int i;

????people per[3];

????per[0].age=20;strcpy(per[0].name,"li");

????per[1].age=18;strcpy(per[1].name,"wang");

????per[2].age=21;strcpy(per[2].name,"zhang");

?

????if((pFile = fopen ("myfile.txt", "wb"))==NULL)

????{

????????printf("cant open the file");

????????exit(0);

????}

?

????for(i=0;i<3;i )

????{

????????if(fwrite(&per[i],sizeof(people),1,pFile)!=1)

????????????printf("file write error\n");

????}

????fclose (pFile);

????return 0;

}

(2)讀結構體數據

#include?

#include?

#include?

typedef struct{

????int age;

????char name[30];

}people;

?

int main () {

????FILE * fp;

????people per;

????if((fp=fopen("myfile.txt","rb"))==NULL)

????{

??????printf("cant open the file");

??????exit(0);

????}

?

????while(fread(&per,sizeof(people),1,fp)==1)?? //如果讀到數據,就顯示;否則退出

????{

????????printf("%d %s\n",per.age,per.name);

????}

????return 0;

}

執行結果:

聲明:

本文于網絡整理,版權歸原作者所有,如來源信息有誤或侵犯權益,請聯系我們刪除或授權事宜。


總結

以上是生活随笔為你收集整理的fwrite函数的一般调用形式是什么?的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。